Esempio n. 1
0
	def run(self, context):
		self.log.info("Touching %s", self.path)
		mkdir(os.path.dirname(self.path))
		path = normLongPath(self.path)

		with openForWrite(path, "wb") as f:
			pass
Esempio n. 2
0
	def _copyFile(self, context, src, dest):
		dest = normLongPath(dest)
		src = normLongPath(src)
		with open(src, 'rb') as s:
			with openForWrite(dest, 'wb') as d:
				for m in self.mappers:
					x = m.getHeader(context)
					if x: 
						self.__unusedMappers.discard(m)
						d.write(x)
				
				for l in s:
					for m in self.mappers:
						prev = l
						l = m.mapLine(context, l)
						if prev != l:
							self.__unusedMappers.discard(m)
						if None == l:
							break
					if None != l:
						d.write(l)

				for m in self.mappers:
					x = m.getFooter(context)
					if x: 
						self.__unusedMappers.discard(m)
						d.write(x)
		shutil.copymode(src, dest)
		assert os.path.exists(dest)
Esempio n. 3
0
    def _copyFile(self, context, src, dest):
        dest = normLongPath(dest)
        src = normLongPath(src)
        with open(src, 'rb') as s:
            with openForWrite(dest, 'wb') as d:
                for m in self.mappers:
                    x = m.getHeader(context)
                    if x:
                        self.__unusedMappers.discard(m)
                        d.write(x)

                for l in s:
                    for m in self.mappers:
                        prev = l
                        l = m.mapLine(context, l)
                        if prev != l:
                            self.__unusedMappers.discard(m)
                        if None == l:
                            break
                    if None != l:
                        d.write(l)

                for m in self.mappers:
                    x = m.getFooter(context)
                    if x:
                        self.__unusedMappers.discard(m)
                        d.write(x)
        shutil.copymode(src, dest)
        assert os.path.exists(dest)
Esempio n. 4
0
 def _copyFile(self, context, src, dest):
     src = normLongPath(src)
     dest = normLongPath(dest)
     with open(src, 'rb') as inp:
         with openForWrite(dest, 'wb') as out:
             shutil.copyfileobj(inp, out)
     shutil.copymode(src, dest)
     assert os.path.exists(dest)
Esempio n. 5
0
	def _copyFile(self, context, src, dest):
		src = normLongPath(src)
		dest = normLongPath(dest)
		with open(src, 'rb') as inp:
			with openForWrite(dest, 'wb') as out:
				shutil.copyfileobj(inp, out)
		shutil.copymode(src, dest)
		assert os.path.exists(dest)
Esempio n. 6
0
	def run(self, context):
		contents = self._getContents(context)
		
		mkdir(os.path.dirname(self.path))
		path = normLongPath(self.path)
		with openForWrite(path, 'wb') as f:
			f.write(contents.replace('\n', os.linesep))
		if self.__mode and not isWindows():
			os.chmod(path, self.__mode)
		if self.__executable and not isWindows():
			os.chmod(path, stat.S_IXOTH | stat.S_IXUSR | stat.S_IXGRP | os.stat(self.path).st_mode)
Esempio n. 7
0
    def run(self, context):
        contents = self._getContents(context)

        mkdir(os.path.dirname(self.path))
        path = normLongPath(self.path)
        with openForWrite(path, 'wb') as f:
            f.write(contents.replace('\n', os.linesep))
        if self.__mode and not isWindows():
            os.chmod(path, self.__mode)
        if self.__executable and not isWindows():
            os.chmod(
                path, stat.S_IXOTH | stat.S_IXUSR | stat.S_IXGRP
                | os.stat(self.path).st_mode)
Esempio n. 8
0
    def run(self, context):
        """
			Calls the wrapped run method
		"""
        implicitInputs = self.__getImplicitInputs(context)
        if implicitInputs or isDirPath(self.target.name):
            deleteFile(self._implicitInputsFile)

        self.target.run(context)

        # if target built successfully, record what the implicit inputs were to help with the next up to date
        # check and ensure incremental build is correct
        if implicitInputs or isDirPath(self.target.name):
            log.debug('writing implicitInputsFile: %s',
                      self._implicitInputsFile)
            mkdir(os.path.dirname(self._implicitInputsFile))
            with openForWrite(toLongPathSafe(self._implicitInputsFile),
                              'wb') as f:
                f.write(os.linesep.join(implicitInputs))
Esempio n. 9
0
	def run(self, context):
		"""
			Calls the wrapped run method
		"""
		implicitInputs = self.__getImplicitInputs(context)
		if implicitInputs or isDirPath(self.target.name):
			deleteFile(self._implicitInputsFile)
		
		self.target.run(context)
		
		# if target built successfully, record what the implicit inputs were to help with the next up to date 
		# check and ensure incremental build is correct
		if implicitInputs or isDirPath(self.target.name):
			log.debug('writing implicitInputsFile: %s', self._implicitInputsFile)
			mkdir(os.path.dirname(self._implicitInputsFile))
			iminpath = os.path.normpath(self._implicitInputsFile)
			if isWindows(): iminpath = u'\\\\?\\'+iminpath
			with openForWrite(iminpath, 'wb') as f:
				f.write(os.linesep.join(implicitInputs))
Esempio n. 10
0
	def run(self, context):
		options = context.mergeOptions(self)
		args = [ options['docker.path'] ]
		environs = { 'DOCKER_HOST' : options['docker.host'] } if options['docker.host'] else {}
		if self.mode == Docker.BUILD:
			dargs = list(args)
			dargs.extend([
					'build', '--rm=true', '-t', context.expandPropertyValues(self.imagename),
				])
			if self.buildArgs: dargs.extend(["--build-arg=%s" % [context.expandPropertyValues(x) for x in self.buildArgs]])
			if self.dockerfile: dargs.extend(["-f", context.expandPropertyValues(self.dockerfile)])
			inputs = self.inputs.resolve(context)
			if len(inputs) != 1: raise BuildException("Must specify a single input for Docker.BUILD", location = self.location)
			dargs.append(inputs[0])
			cwd = os.path.dirname(inputs[0])
			call(dargs, outputHandler=options['docker.processoutputhandler']('docker-build', False, options=options), timeout=options['process.timeout'], env=environs, cwd=cwd)
		elif self.mode == Docker.PUSHTAG:
			inputs = self.inputs.resolve(context)
			if len(inputs) != 0: raise BuildException("Must not specify inputs for Docker.PUSHTAG", location = self.location)
			dargs = list(args)
			dargs.extend([
					'tag', context.expandPropertyValues(self.depimage), context.expandPropertyValues(self.imagename),
				])
			call(dargs, outputHandler=options['docker.processoutputhandler']('docker-tag', False, options=options), timeout=options['process.timeout'], env=environs)
			dargs = list(args)
			dargs.extend([
					'push', context.expandPropertyValues(self.imagename),
				])
			call(dargs, outputHandler=options['docker.processoutputhandler']('docker-push', False, options=options), timeout=options['process.timeout'], env=environs)
		else:
			raise BuildException('Unknown Docker mode. Must be Docker.BUILD or Docker.PUSHTAG', location = self.location)
		
		# update the stamp file
		path = normLongPath(self.path)
		mkdir(os.path.dirname(path))
		with openForWrite(path, 'wb') as f:
			pass
Esempio n. 11
0
    def run(self, context):
        self.keystore = context.expandPropertyValues(self.keystore)
        options = self.options

        mkdir(self.path)
        for src, dest in self.jars.resolveWithDestinations(context):
            if '..' in dest:
                # to avoid people abusing this to copy files outside the dest directory!
                raise Exception(
                    'This target does not permit destination paths to contain ".." relative path expressions'
                )

            try:
                with open(src, 'rb') as s:
                    with openForWrite(os.path.join(self.path, dest),
                                      'wb') as d:
                        d.write(s.read())

                shutil.copystat(src, os.path.join(self.path, dest))

                # When we re-jar with the user specified manifest entries, jar will complain
                # about duplicate attributes IF the original MANIFEST.MF already has those entries.
                # This is happening for latest version of SL where Application-Name, Permission etc
                # were already there.
                #
                # The block of code below will first extract the original MANIFEST.MF from the source
                # jar file, read all manifest entry to a list.  When constructing the new manifest entries,
                # make sure the old MANIFEST.MF doesn't have that entry before putting the new manifest entry
                # to the list.  This will avoid the duplicate attribute error.
                #

                if self.manifestDefaults:

                    lines = []

                    # read each line of MANIFEST.MF of the original jar and put them in lines
                    with zipfile.ZipFile(src, 'r') as zf:
                        lst = zf.infolist()
                        for zi in lst:
                            fn = zi.filename
                            if fn.lower().endswith('manifest.mf'):
                                try:
                                    manifest_txt = zf.read(zi.filename)
                                except Exception, e:
                                    raise BuildException(
                                        'Failed reading the manifest file %s with exception:%s'
                                        % (fn, e))

                                # if we have all manifest text, parse and save each line
                                if manifest_txt:
                                    # CR LF | LF | CR  can be there as line feed and hence the code below
                                    lines = manifest_txt.replace(
                                        '\r\n', '\n').replace('\r',
                                                              '\n').split('\n')

                                # done
                                break

                    original_entries = collections.OrderedDict(
                    )  # to ensure we don't overwrite/duplicate these
                    # populate the manifest_entries with original values from original manifest
                    for l in lines:
                        if ':' in l and not l.startswith(
                                ' '
                        ):  # ignore continuation lines etc because keys are all we care about
                            key, value = l.split(':', 1)
                            original_entries[key] = value.strip()

                    # build up a list of the new manifest entries (will be merged into any existing manifest by jar)
                    manifest_entries = collections.OrderedDict()
                    for i in self.manifestDefaults:
                        # if entry isn't there yet, add to the list
                        if i not in original_entries:
                            manifest_entries[i] = context.expandPropertyValues(
                                self.manifestDefaults[i])

                    # create the manifest file
                    # we want to add the manifest entries explicitly specified here but
                    # NOT the 'default' manifest entries we usually add, since these
                    # are likely to have been set already, and we do not want duplicates
                    mkdir(self.workDir)
                    manifest = os.path.join(self.workDir,
                                            "MANIFEST.MF")  # manifest file

                    options = dict(options)
                    options['jar.manifest.defaults'] = {}
                    create_manifest(manifest, manifest_entries, options)

                    # update the EXISTING jar file with the new manifest entries, which will be merged into
                    # existing manifest by the jar tool
                    jar(os.path.join(self.path, dest),
                        manifest,
                        None,
                        options,
                        update=True)

                signjar(os.path.join(self.path, dest),
                        self.keystore,
                        options,
                        alias=self.alias,
                        storepass=self.storepass,
                        outputHandler=ProcessOutputHandler(
                            'signjars',
                            treatStdErrAsErrors=False,
                            options=options))
            except BuildException, e:
                raise BuildException('Error processing %s: %s' %
                                     (os.path.basename(dest), e))
Esempio n. 12
0
    def run(self, context):
        options = self.options

        # make sure temp dir exists
        mkdir(self.workDir)

        classes = os.path.join(self.workDir,
                               "classes")  # output dir for classes

        # create the classpath, sorting within PathSet (for determinism), but retaining original order of
        # PathSet elements in the list
        classpath = os.pathsep.join(self.classpath.resolve(context))

        # compile everything
        mkdir(
            classes
        )  # (need this for assembling other files to package later on, even if we don't do any javac)
        if self.compile:
            mkdir(self.getOption('javac.logs'))
            javac(classes,
                  self.compile.resolve(context),
                  classpath,
                  options=options,
                  logbasename=options.get('javac.logs') + '/' +
                  targetNameToUniqueId(self.name),
                  targetname=self.name)

        manifest = os.path.join(self.workDir, "MANIFEST.MF")  # manifest file

        if isinstance(self.manifest, basestring):
            manifest = context.getFullPath(self.manifest, self.baseDir)
        elif self.manifest == None:
            manifest = None
        else:  # generate one
            # rewrite property values in the manifest
            manifest_entries = {}
            for i in self.manifest:
                manifest_entries[i] = context.expandPropertyValues(
                    self.manifest[i])

            # determine classpath for manifest
            classpath_entries = []

            if "Class-path" not in manifest_entries:  # assuming it wasn't hardcoded, set it here
                for src, dest in self.classpath.resolveWithDestinations(
                        context):
                    # we definitely do want to support use of ".." in destinations here, it can be very useful
                    classpath_entries.append(dest)
                assert isinstance(
                    options['jar.manifest.classpathAppend'], list), options[
                        'jar.manifest.classpathAppend']  # must not be a string
                classpath_entries.extend(
                    options['jar.manifest.classpathAppend'] or [])

                # need to always use / not \ for these to be valid
                classpath_entries = [
                    p.replace(os.path.sep, '/').replace('\\', '/')
                    for p in classpath_entries if p
                ]

                if classpath_entries:
                    manifest_entries["Class-path"] = " ".join(
                        classpath_entries)  # include the classpath from here
            if not manifest_entries.get(
                    'Class-path'
            ):  # suppress this element entirely if not needed, otherwise there would be no way to have an empty classpath
                manifest_entries.pop('Class-path', '')

            # create the manifest file
            create_manifest(manifest, manifest_entries, options=options)

        # copy in the additional things to include
        for (src, dest) in self.package.resolveWithDestinations(context):
            if '..' in dest:
                raise Exception(
                    'This target does not permit packaged destination paths to contain ".." relative path expressions'
                )
            mkdir(os.path.dirname(os.path.join(classes, dest)))
            destpath = normLongPath(classes + '/' + dest)
            srcpath = normLongPath(src)

            if os.path.isdir(srcpath):
                mkdir(destpath)
            else:
                with open(srcpath, 'rb') as s:
                    with openForWrite(destpath, 'wb') as d:
                        d.write(s.read())

        # create the jar
        jar(self.path,
            manifest,
            classes,
            options=options,
            preserveManifestFormatting=self.preserveManifestFormatting,
            outputHandler=ProcessOutputHandler('jar',
                                               treatStdErrAsErrors=False,
                                               options=options))
Esempio n. 13
0
class CompilerMakeDependsPathSet(BasePathSet):
	"""
		Use the selection ToolChain to get a list of dependencies from a set of source files
	"""
	def __init__(self, target, src, flags=None, includes=None):
		"""
		@param target: the BaseTarget object for which this path set is being caculated

		@param src: a PathSet of source file paths

		@param flags: additional compiler flags

		@param includes: a list of include directory paths
		"""
		BasePathSet.__init__(self)
		self.log = makedeplog
		self.target = target
		self.sources = src
		self.flags = flatten([flags]) or []
		self.includes = includes or []
		
	def __repr__(self):
		return "MakeDepend(%s, %s)" % (self.sources, self.flags)
	def resolveWithDestinations(self, context):
		return [(i, os.path.basename(i)) for i in _resolveUnderlyingDependencies(context)]
	def clean(self):
		dfile = self.target.workDir+'.makedepend'
		deleteFile(dfile)
	def _resolveUnderlyingDependencies(self, context):
		deplist = None
		options = self.target.options # get the merged options

		dfile = normLongPath(self.target.workDir+'.makedepend')
		testsources = self.sources.resolve(context)
		depsources = self.sources._resolveUnderlyingDependencies(context)

		needsRebuild = not os.path.exists(dfile)
		if needsRebuild:
			self.log.info("Rebuilding dependencies for %s because cached dependencies file does not exist (%s)" % (self.target, dfile))
		dfiletime = 0 if needsRebuild else getmtime(dfile) 
		for x in testsources:
			if not exists(x):
				# can't generate any deps if some source files don't yet exist
				self.log.info("Dependency generation %s postponed because source file does not exist: %s" % (self.target, x))
				return depsources
			elif getmtime(x) > dfiletime:
				if not needsRebuild:	self.log.info("Rebuilding dependencies for %s because cached dependencies file is older than %s" % (self.target, x))
				needsRebuild = True

		if not needsRebuild: # read in cached dependencies
			deplist = []
			with open(dfile) as f:
				lines = f.readlines()
				header = lines[0].strip()
				lines = lines[1:]
				for d in lines:
					d = d.strip()
					if context._isValidTarget(d) or exists(normLongPath(d)):
						deplist.append(d)
					else:
						needsRebuild = True
						self.log.warn("Rebuilding dependencies for %s because dependency %s is missing" % (self.target, d))
						break
			if header != str(self):
				self.log.info("Rebuilding dependencies for %s because target options have changed (%s != %s)" % (self.target, header, str(self)))
			elif not needsRebuild:
				return deplist

		# generate them again
		startt = time.time()
		self.log.info("*** Generating native dependencies for %s" % self.target)
		try:
			deplist = options['native.compilers'].dependencies.depends(context=context, src=testsources, options=options, flags=flatten(options['native.cxx.flags']+[context.expandPropertyValues(x).split(' ') for x in self.flags]), includes=flatten(self.includes.resolve(context)+[context.expandPropertyValues(x, expandList=True) for x in options['native.include']]))
		except BuildException, e:
			if len(testsources)==1 and testsources[0] not in str(e):
				raise BuildException('Dependency resolution failed for %s: %s'%(testsources[0], e))
			raise
		deplist += depsources
		mkdir(os.path.dirname(dfile))
		with openForWrite(dfile, 'wb') as f:
			assert not os.linesep in str(self)
			f.write(str(self)+os.linesep)
			for d in deplist:
				f.write(d.encode('UTF-8')+os.linesep)
		if time.time()-startt > 5: # this should usually be pretty quick, so may indicate a real build file mistake
			self.log.warn('Dependency generation took a long time: %0.1f s to evaluate %s', time.time()-startt, self)

		return deplist
Esempio n. 14
0
 def updateStampFile(self):
     """ Assumes self.path is a stamp file that just needs creating / timestamp updating and does so """
     path = normLongPath(self.path)
     mkdir(os.path.dirname(path))
     with openForWrite(path, 'wb') as f:
         pass
Esempio n. 15
0
	def run(self, context):
		self.keystore = context.expandPropertyValues(self.keystore)
		options = context.mergeOptions(self) # get the merged options

		mkdir(self.path)
		for src, dest in self.jars.resolveWithDestinations(context):
			try:
				with open(src, 'rb') as s:
					with openForWrite(os.path.join(self.path, dest), 'wb') as d:
						d.write(s.read())

				shutil.copystat(src, os.path.join(self.path, dest))
				
				# When we re-jar with the user specified manifest entries, jar will complain
				# about duplicate attributes IF the original MANIFEST.MF already has those entries.
				# This is happening for latest version of SL where Application-Name, Permission etc
				# were already there.
				#
				# The block of code below will first extract the original MANIFEST.MF from the source
				# jar file, read all manifest entry to a list.  When constructing the new manifest entries,
				# make sure the old MANIFEST.MF doesn't have that entry before putting the new manifest entry
				# to the list.  This will avoid the duplicate attribute error.
				#  
			
				if self.manifestDefaults:
					
					lines = []
					
					# read each line of MANIFEST.MF of the original jar and put them in lines
					with zipfile.ZipFile(src, 'r') as zf:
						lst = zf.infolist()
						for zi in lst:
							fn = zi.filename
							if fn.lower().endswith('manifest.mf'):
								try:
									manifest_txt = zf.read(zi.filename)
								except Exception, e:
									raise BuildException('Failed reading the manifest file %s with exception:%s' % (fn, e))

								# if we have all manifest text, parse and save each line
								if manifest_txt:
									# CR LF | LF | CR  can be there as line feed and hence the code below
									lines = manifest_txt.replace('\r\n', '\n').replace('\r','\n').split('\n')
										
								# done
								break
						
					
					original_entries = collections.OrderedDict()  # to ensure we don't overwrite/duplicate these
					# populate the manifest_entries with original values from original manifest
					for l in lines:
						if ':' in l and not l.startswith(' '): # ignore continuation lines etc because keys are all we care about
							key,value = l.split(':', 1)
							original_entries[key] = value.strip()
					
					# build up a list of the new manifest entries (will be merged into any existing manifest by jar)
					manifest_entries = collections.OrderedDict()
					for i in self.manifestDefaults:
						# if entry isn't there yet, add to the list
						if i not in original_entries:
							manifest_entries[i] = context.expandPropertyValues(self.manifestDefaults[i])
		
					# create the manifest file
					# we want to add the manifest entries explicitly specified here but 
					# NOT the 'default' manifest entries we usually add, since these 
					# are likely to have been set already, and we do not want duplicates
					mkdir(self.workDir)
					manifest = os.path.join(self.workDir, "MANIFEST.MF") # manifest file

					options['jar.manifest.defaults'] = {}
					create_manifest(manifest, manifest_entries, options)
	
					# update the EXISTING jar file with the new manifest entries, which will be merged into 
					# existing manifest by the jar tool
					jar(os.path.join(self.path, dest), manifest, None, options, update=True)
	
				signjar(os.path.join(self.path, dest), self.keystore, options, alias=self.alias, storepass=self.storepass, 
					outputHandler=ProcessOutputHandler('signjars', treatStdErrAsErrors=False, options=options))
			except BuildException, e:
				raise BuildException('Error processing %s: %s'%(os.path.basename(dest), e))
Esempio n. 16
0
	def run(self, context):
		options = context.mergeOptions(self) # get the merged options

		# make sure temp dir exists
		mkdir(self.workDir)

		classes = os.path.join(self.workDir, "classes") # output dir for classes
		
		# create the classpath, sorting within PathSet (for determinism), but retaining original order of 
		# PathSet elements in the list
		classpath = os.pathsep.join(self.classpath.resolve(context)) 

		# compile everything
		mkdir(classes) # (need this for assembling other files to package later on, even if we don't do any javac)
		if self.compile:
			mkdir(options.get('javac.logs'))
			javac(classes, self.compile.resolve(context), classpath, options=options, logbasename=options.get('javac.logs')+'/'+targetNameToUniqueId(self.name), targetname=self.name)

		manifest = os.path.join(self.workDir, "MANIFEST.MF") # manifest file
	
		if isinstance(self.manifest, basestring):
			manifest = context.getFullPath(self.manifest, self.baseDir)
		elif self.manifest == None:
			manifest = None
		else: # generate one
			# rewrite property values in the manifest
			manifest_entries = {}
			for i in self.manifest:
				manifest_entries[i] = context.expandPropertyValues(self.manifest[i])
	
			# determine classpath for manifest
			classpath_entries = []
			
			if "Class-path" not in manifest_entries: # assuming it wasn't hardcoded, set it here
				for src, dest in self.classpath.resolveWithDestinations(context):
					classpath_entries.append(dest)
				assert isinstance(options['jar.manifest.classpathAppend'], list), options['jar.manifest.classpathAppend'] # must not be a string
				classpath_entries.extend(options['jar.manifest.classpathAppend'] or [])
				
				# need to always use / not \ for these to be valid
				classpath_entries = [p.replace(os.path.sep, '/').replace('\\', '/') for p in classpath_entries if p]
				
				if classpath_entries:
					manifest_entries["Class-path"] = " ".join(classpath_entries) # include the classpath from here
			if not manifest_entries.get('Class-path'): # suppress this element entirely if not needed, otherwise there would be no way to have an empty classpath
				manifest_entries.pop('Class-path','')
			
			# create the manifest file
			create_manifest(manifest, manifest_entries, options=options)

		# copy in the additional things to include
		for (src, dest) in self.package.resolveWithDestinations(context):
			mkdir(os.path.dirname(os.path.join(classes, dest)))
			destpath = normLongPath(classes+'/'+dest)
			srcpath = normLongPath(src)

			if os.path.isdir(srcpath):
				mkdir(destpath)
			else:
				with open(srcpath, 'rb') as s:
					with openForWrite(destpath, 'wb') as d:
						d.write(s.read())

		# create the jar
		jar(self.path, manifest, classes, options=options, preserveManifestFormatting=self.preserveManifestFormatting, 
			outputHandler=ProcessOutputHandler('jar', treatStdErrAsErrors=False,options=options))