# Path: python/gup/log.py
# def getLogger(*a):
# 	logger = logging.getLogger(*a)
# 	logger.addFilter(_color_filter)
# 	return logger
# 
# Path: python/gup/gupfile.py
# class Builder(object):
# 	'''
# 	The canonical builder for a target.
# 	`path` is the path to the build script, even if this
# 	builder was obtained indirectly (via a Gupfile match)
# 	'''
# 	def __init__(self, script_path, target, basedir, parent):
# 		self.path = script_path
# 		self.realpath = os.path.realpath(self.path)
# 		self.target = target
# 		self.basedir = basedir
# 		self.target_path = os.path.join(self.basedir, self.target)
# 		self.parent = parent
# 	
# 	def __repr__(self):
# 		return "Builder(path=%r, target=%r, basedir=%r, parent=%r)" % (self.path, self.target, self.basedir, self.parent)
# 
# 	@staticmethod
# 	def for_target(path):
# 		for candidate in possible_gup_files(path):
# 			builder = candidate.get_builder()
# 			if builder is not None:
# 				return builder
# 		return None
# 
# Path: python/gup/parallel.py
# class Lock:
# 	def __init__(self, name):
# 		self.owned = False
# 		self.name  = name
# 		self.lockfile = os.open(self.name, os.O_RDWR | os.O_CREAT, 0o666)
# 		_close_on_exec(self.lockfile, True)
# 		self.shared = fcntl.LOCK_SH
# 		self.exclusive = fcntl.LOCK_EX
# 
# 	def __del__(self):
# 		if self.owned:
# 			self.unlock()
# 		os.close(self.lockfile)
# 
# 	def read(self):
# 		return LockHelper(self, fcntl.LOCK_SH)
# 
# 	def write(self):
# 		return LockHelper(self, fcntl.LOCK_EX)
# 
# 	def trylock(self, kind=fcntl.LOCK_EX):
# 		assert(self.owned != kind)
# 		try:
# 			fcntl.lockf(self.lockfile, kind|fcntl.LOCK_NB, 0, 0)
# 		except IOError as e:
# 			if e.errno in (errno.EAGAIN, errno.EACCES):
# 				_log.trace("%s lock failed", self.name)
# 				pass  # someone else has it locked
# 			else:
# 				raise
# 		else:
# 			_log.trace("%s lock (try)", self.name)
# 			self.owned = kind
# 
# 	def waitlock(self, kind=fcntl.LOCK_EX):
# 		assert(self.owned != kind)
# 		_log.trace("%s lock (wait)", self.name)
# 		fcntl.lockf(self.lockfile, kind, 0, 0)
# 		self.owned = kind
# 
# 	def unlock(self):
# 		if not self.owned:
# 			raise Exception("can't unlock %r - we don't own it" % self.name)
# 		fcntl.lockf(self.lockfile, fcntl.LOCK_UN, 0, 0)
# 		_log.trace("%s unlock", self.name)
# 		self.owned = False
# 
# Path: python/gup/path.py
# def resolve_base(p):
# 	return os.path.join(
# 		os.path.realpath(os.path.dirname(p)),
# 		os.path.basename(p)
# 	)
# 
# Path: python/gup/var.py
# RUN_ID = os.environ['GUP_RUNID'] = str(int(time.time() * 1000))
# 
# Path: python/gup/error.py
# class SafeError(Exception):
# 	exitcode = 10
# 	pass


import os
import logging
import errno
			import hashlib
from .util import *
from .log import getLogger
from .gupfile import Builder
from .parallel import Lock
from .path import resolve_base
from .var import RUN_ID
from .error import SafeError
		return cls(int(mtime))
	
	def is_dirty(self, args):
		path = args.deps.path

		mtime = get_mtime(path)
		assert mtime is not None
		_log.debug("comparing stored mtime %s to %s", self.value, mtime)
		if mtime != self.value:
			log_method = _log.warning
			if os.path.isdir(path):
				# dirs are modified externally for various reasons, not worth warning
				log_method = _log.debug
			log_method("%s was externally modified - rebuilding" % (path,))
			return True
		return False

class RunId(Dependency):
	tag = 'run:'
	num_fields = 1

	def __init__(self, runid):
		self.value = runid
		self.fields = [runid]

	@classmethod
	def current(cls):
		return cls(RUN_ID)

	def is_current(self):

# Path: python/gup/log.py
# def getLogger(*a):
# 	logger = logging.getLogger(*a)
# 	logger.addFilter(_color_filter)
# 	return logger
# 
# Path: python/gup/gupfile.py
# class Builder(object):
# 	'''
# 	The canonical builder for a target.
# 	`path` is the path to the build script, even if this
# 	builder was obtained indirectly (via a Gupfile match)
# 	'''
# 	def __init__(self, script_path, target, basedir, parent):
# 		self.path = script_path
# 		self.realpath = os.path.realpath(self.path)
# 		self.target = target
# 		self.basedir = basedir
# 		self.target_path = os.path.join(self.basedir, self.target)
# 		self.parent = parent
# 	
# 	def __repr__(self):
# 		return "Builder(path=%r, target=%r, basedir=%r, parent=%r)" % (self.path, self.target, self.basedir, self.parent)
# 
# 	@staticmethod
# 	def for_target(path):
# 		for candidate in possible_gup_files(path):
# 			builder = candidate.get_builder()
# 			if builder is not None:
# 				return builder
# 		return None
# 
# Path: python/gup/parallel.py
# class Lock:
# 	def __init__(self, name):
# 		self.owned = False
# 		self.name  = name
# 		self.lockfile = os.open(self.name, os.O_RDWR | os.O_CREAT, 0o666)
# 		_close_on_exec(self.lockfile, True)
# 		self.shared = fcntl.LOCK_SH
# 		self.exclusive = fcntl.LOCK_EX
# 
# 	def __del__(self):
# 		if self.owned:
# 			self.unlock()
# 		os.close(self.lockfile)
# 
# 	def read(self):
# 		return LockHelper(self, fcntl.LOCK_SH)
# 
# 	def write(self):
# 		return LockHelper(self, fcntl.LOCK_EX)
# 
# 	def trylock(self, kind=fcntl.LOCK_EX):
# 		assert(self.owned != kind)
# 		try:
# 			fcntl.lockf(self.lockfile, kind|fcntl.LOCK_NB, 0, 0)
# 		except IOError as e:
# 			if e.errno in (errno.EAGAIN, errno.EACCES):
# 				_log.trace("%s lock failed", self.name)
# 				pass  # someone else has it locked
# 			else:
# 				raise
# 		else:
# 			_log.trace("%s lock (try)", self.name)
# 			self.owned = kind
# 
# 	def waitlock(self, kind=fcntl.LOCK_EX):
# 		assert(self.owned != kind)
# 		_log.trace("%s lock (wait)", self.name)
# 		fcntl.lockf(self.lockfile, kind, 0, 0)
# 		self.owned = kind
# 
# 	def unlock(self):
# 		if not self.owned:
# 			raise Exception("can't unlock %r - we don't own it" % self.name)
# 		fcntl.lockf(self.lockfile, fcntl.LOCK_UN, 0, 0)
# 		_log.trace("%s unlock", self.name)
# 		self.owned = False
# 
# Path: python/gup/path.py
# def resolve_base(p):
# 	return os.path.join(
# 		os.path.realpath(os.path.dirname(p)),
# 		os.path.basename(p)
# 	)
# 
# Path: python/gup/var.py
# RUN_ID = os.environ['GUP_RUNID'] = str(int(time.time() * 1000))
# 
# Path: python/gup/error.py
# class SafeError(Exception):
# 	exitcode = 10
# 	pass


# Path: python/gup/state.py
import os
import logging
import errno
			import hashlib
from .util import *
from .log import getLogger
from .gupfile import Builder
from .parallel import Lock
from .path import resolve_base
from .var import RUN_ID
from .error import SafeError

		return cls(int(mtime))
	
	def is_dirty(self, args):
		path = args.deps.path

		mtime = get_mtime(path)
		assert mtime is not None
		_log.debug("comparing stored mtime %s to %s", self.value, mtime)
		if mtime != self.value:
			log_method = _log.warning
			if os.path.isdir(path):
				# dirs are modified externally for various reasons, not worth warning
				log_method = _log.debug
			log_method("%s was externally modified - rebuilding" % (path,))
			return True
		return False

class RunId(Dependency):
	tag = 'run:'
	num_fields = 1

	def __init__(self, runid):
		self.value = runid
		self.fields = [runid]

	@classmethod
	def current(cls):
		return cls(RUN_ID)

	def is_current(self):

		return self.value == RUN_ID