Ejemplo n.º 1
0
    def create(self, output_path, dry_run=False, output_format=None):
        """
        Create the archive at output_file_path.

        Type of the archive is determined either by extension of output_file_path or by output_format.
        Supported formats are: gz, zip, bz2, tar, tgz

        @param output_path: Output file path.
        @type output_path: string

        @param dry_run: Determines whether create should do nothing but print what it would archive.
        @type dry_run: bool

        @param output_format: Determines format of the output archive. If None, format is determined from extension
            of output_file_path.
        @type output_format: string
        """
        if output_format is None:
            file_name, file_ext = path.splitext(output_path)
            output_format = file_ext[len(extsep):].lower()
            self.LOG.debug("Output format is not explicitly set, determined format is {0}.".format(output_format))

        if not dry_run:
            if output_format == 'zip':
                archive = ZipFile(path.abspath(output_path), 'w')

                def add_file(file_path, arcname):
                    if not path.islink(file_path):
                        archive.write(file_path, arcname, ZIP_DEFLATED)
                    else:
                        i = ZipInfo(arcname)
                        i.create_system = 3
                        i.external_attr = 0xA1ED0000
                        archive.writestr(i, readlink(file_path))
            elif output_format in ['tar', 'bz2', 'gz', 'tgz']:
                if output_format == 'tar':
                    t_mode = 'w'
                elif output_format == 'tgz':
                    t_mode = 'w:gz'
                else:
                    t_mode = 'w:{0}'.format(output_format)

                archive = tarfile.open(path.abspath(output_path), t_mode)
                add_file = lambda file_path, arcname: archive.add(file_path, arcname)
            else:
                raise RuntimeError("Unknown format: {0}".format(output_format))

            def archiver(file_path, arcname):
                self.LOG.debug("Compressing {0} => {1}...".format(file_path, arcname))
                add_file(file_path, arcname)
        else:
            archive = None
            archiver = lambda file_path, arcname: self.LOG.info("{0} => {1}".format(file_path, arcname))

        self.archive_all_files(archiver)

        if archive is not None:
            archive.close()
Ejemplo n.º 2
0
    def create(self, output_file):
        """
        create(str output_file) -> None
        
        Creates the archive, written to the given output_file
        Filetype may be one of:  gz, zip, bz2, tar, tgz
        """
        #
        # determine the format
        #
        _, _, format = output_file.rpartition(".")
        format = format.lower()

        if format == 'zip':
            from zipfile import ZipFile, ZIP_DEFLATED
            output_archive = ZipFile(path.abspath(output_file), 'w')
            add = lambda name, arcname: output_archive.write(
                name, self.prefix + arcname, ZIP_DEFLATED)

        elif format in ['tar', 'bz2', 'gz', 'tgz']:
            import tarfile

            if format == 'tar':
                t_mode = 'w'
            elif format == 'tgz':
                t_mode = 'w:gz'
            else:
                t_mode = ('w:%s' % format)

            output_archive = tarfile.open(path.abspath(output_file), t_mode)
            add = lambda name, arcname: output_archive.add(
                name, self.prefix + arcname)
        else:
            raise RuntimeError("Unknown format: '%s'" % format)

        #
        # compress
        #

        # extra files first (we may change folder later)
        for name in self.extra:
            if self.verbose:
                toPath = '=> %s%s' % (self.prefix, name) if self.prefix else ""
                print 'Compressing %s %s ...' % (name, toPath)
            add(name, name)

        self._excludes = []

        for name, arcname in self.listFiles(path.abspath('')):
            if self.verbose:
                toPath = '=> %s%s' % (self.prefix,
                                      arcname) if self.prefix else ""
                print 'Compressing %s %s ...' % (arcname, toPath)
            add(name, arcname)

        output_archive.close()
Ejemplo n.º 3
0
    def create(self, output_file):
        """
        create(str output_file) -> None
        
        Creates the archive, written to the given output_file
        Filetype may be one of:  gz, zip, bz2, tar, tgz
        """
        #
        # determine the format
        #
        _, _, format = output_file.rpartition(".")
        format = format.lower()

        if format == 'zip':
            from zipfile import ZipFile, ZIP_DEFLATED
            output_archive = ZipFile(path.abspath(output_file), 'w')
            add = lambda name, arcname: output_archive.write(name, self.prefix + arcname, ZIP_DEFLATED)
    
        elif format in ['tar', 'bz2', 'gz', 'tgz']:
            import tarfile

            if format == 'tar':
                t_mode = 'w'
            elif format == 'tgz':
                t_mode = 'w:gz'
            else:
                t_mode = ('w:%s' % format)
               
            output_archive = tarfile.open(path.abspath(output_file), t_mode)
            add = lambda name, arcname: output_archive.add(name, self.prefix + arcname)
        else:
            raise RuntimeError("Unknown format: '%s'" % format)
        
        #
        # compress
        #
        
        # extra files first (we may change folder later)
        for name in self.extra:
            if self.verbose: 
                toPath = '=> %s%s' % (self.prefix, name) if self.prefix else ""
                print 'Compressing %s %s ...' % (name, toPath)
            add(name, name)
        
        self._excludes = []
        
        for name, arcname in self.listFiles(path.abspath('')):
            if self.verbose: 
                toPath = '=> %s%s' % (self.prefix, arcname) if self.prefix else ""
                print 'Compressing %s %s ...' % (arcname, toPath)
            add(name, arcname)
      
        output_archive.close()
Ejemplo n.º 4
0
    def _createZip(self, outputFile):
        """
        Helper method to create a ZIP file.

        :param outputFile: The target location for the new zip file.
        :type outputFile: `FilePath`
        :return: `ZipFile`
        """
        def zip_writer(dirpath, zippath):
            basedir = os.path.dirname(dirpath) + os.sep
            entry = ZipInfo()
            entry.compress_type = compression

            if os.path.isdir(dirpath):
                for root, dirs, files in os.walk(dirpath):
                    if os.path.basename(root).startswith('.'):
                        # skip hidden directories
                        continue
                    dirname = root.replace(basedir, '')
                    for f in files:
                        if f[-1] == '~' or f.startswith('.'):
                            # skip backup files and all hidden files
                            continue
                        src = root + '/' + f
                        entry = ZipInfo()
                        entry.compress_type = compression
                        entry.filename = dirname + '/' + f
                        entry.date_time = localtime(os.path.getmtime(src))[:6]

                        # hacky
                        if dirname.startswith("html"):
                            if self.source == True:
                                entry.filename = dirname.replace('html', 'doc', 1) + "/" + f
                            else:
                                entry.filename = dirname.replace('html/', '', 1) + "/" + f
                                entry.filename = entry.filename.replace('html/', '', 1)
                        if entry.filename.startswith("examples"):
                            entry.filename = "tutorials/" + entry.filename

                        file_data = open( src, 'rb').read()
                        self.package.writestr(entry, file_data)
            else:
                # top files
                entry.date_time = localtime(os.path.getmtime(dirpath))[:6]
                entry.filename = os.path.basename(zippath)
                file_data = open( dirpath, 'rb').read()
                self.package.writestr(entry, file_data)

        zipfile = ZipFile(outputFile.path, 'w')
        zipfile.add = zip_writer

        return zipfile
Ejemplo n.º 5
0
def write_tarball(folder, wd, name, version, files, format, verbose=False, clobber=False):
	"""
	Write a new tarball to the designated folder
	
	Also name, version, included files are passed
	
	folder: output tarball in this folder; a string object.
	wd: working directory, where the pkg.spec file is; a string object.
	format: "gz", "bz2", "tar" or "zip" to choose output format; a string object.
	"""
	
	formats = {
		"gz": (".tar.gz", "w:gz"),
		"bz2" : (".tar.bz2", "w:bz2"),
		"tar" : (".tar", "w:"),
		"zip" : (".zip", "w")
	}
	
	try:
		extension, compress_mode = formats[format]
	except KeyError:
		raise Exception("Error: No such format %s" % format)
		
	basename = "%s-%s%s" % (name, version, extension)
	out_file = path.join(folder, basename)
	
	if not clobber:
		if path.exists(out_file):
			raise Exception("Error: Output %s already exists!" % basename)
	
	
	if "zip" in format:
		from zipfile import ZipFile, ZIP_DEFLATED
		zip_file = ZipFile(out_file, mode=compress_mode, compression=ZIP_DEFLATED)
		context = (zip_file, wd, verbose)
		write_member = lambda abs, rel: write_zip_file(context, abs, rel)
	else:
		from tarfile import TarFile
		zip_file = TarFile.open(out_file, mode=compress_mode)
		write_member = lambda abs, rel: zip_file.add(abs, arcname=rel)
	
	print "Creating", basename
	for rel_path in files:
		if verbose:
			print "Writing", rel_path
		abs_path = path.join(wd, rel_path)
		write_member(abs_path, rel_path)
	zip_file.close()
Ejemplo n.º 6
0
def make_archive(path, data_to_archive):
    """
    Create archive with certain data

    :param path: Path to archive file (ex: /home/user/archive.tar)
    :type path: pathlib.Path

    :param data_to_archive: list of dirs/files for archiving
    Example:
        data_to_archive = [
            {
                'from_path': '/home/mediasdk/openSource/build_scripts/root_dir/repos/MediaSDK/__cmake/intel64.make.release',
                'relative': [
                    {
                        'path': '__bin',
                        'pack_as': 'bin'
                    },
                    {
                        'path': '__lib',
                        'pack_as': 'lib'
                    }
                ]
            },
            {
                'from_path': '/home/mediasdk/openSource/build_scripts/root_dir/repos/MediaSDK',
                'relative': [
                    {
                        'path': 'tests'
                    },
                    {
                        'path': 'CMakeLists.txt'
                    },
                ]
            }
        ]
    :type data_to_archive: List

    :return: None
    """

    no_errors = True

    log = logging.getLogger('helper.make_archive')

    log.info('-' * 50)
    log.info('create archive %s', path)

    if path.suffix == '.tar':
        pkg = tarfile.open(path, "w")
    elif path.suffix == '.gz':
        pkg = tarfile.open(path, "w:gz", compresslevel=6)
    elif path.suffix == '.bz2':
        pkg = tarfile.open(path, "w:bz2")
    elif path.suffix == '.zip':
        pkg = ZipFile(path, 'w', compression=ZIP_DEFLATED)
    else:
        log.error("Extension %s is not supported", path.suffix)
        no_errors = False

    for info in data_to_archive:
        for relative in info['relative']:
            path_to_archive = info['from_path'] / relative['path']
            pack_as = pathlib.Path(relative.get('pack_as', relative['path']))

            log.info('add to archive %s, pack as "%s"', path_to_archive,
                     pack_as)
            try:
                if path.suffix in ('.tar', '.gz'):
                    pkg.add(path_to_archive, arcname=pack_as)
                elif path.suffix == '.zip':
                    _zip_data(path_to_archive, pack_as, pkg)
            except:
                log.exception("Can not pack results")
                no_errors = False

    pkg.close()

    return no_errors
Ejemplo n.º 7
0
def archive_git(output_file, format='tar', repo_root='.', verbose=False):
    def git_files(repo_root=repo_root, exclude=True, verbose=False, prefix=''):

        excludes = []

        for filepath in Popen(
                'cd %s && git ls-files --cached --full-name --no-empty-directory'
                % (repo_root, ),
                shell=True,
                stdout=PIPE).stdout.read().splitlines():
            filename = path.join(filepath, prefix)
            fullpath = path.join(repo_root, filepath)

            # right now every time we find a .gitattributes, we
            # overwrite the previous patterns. might be nice to
            # use a dictionary that retains the patterns on a
            # parent-path level. But this works fine for a top-level
            # file in the main repo and top level of submodules
            if exclude and filename == '.gitattributes':
                excludes = []
                fh = open(filepath, 'r')
                for line in fh:
                    if not line: break
                    tokens = line.strip().split()
                    if 'export-ignore' in tokens[1:]:
                        excludes.append(tokens[0])
                fh.close()

            if not filename.startswith('.git') and not path.isdir(filepath):

                # check the patterns first
                ignore = False
                for pattern in excludes:
                    if fnmatch(filepath, pattern) or fnmatch(
                            filename, pattern):
                        if verbose:
                            print 'Exclude pattern matched (%s): %s' % (
                                pattern, filepath)
                        ignore = True
                        break
                if ignore:
                    continue

                # baselevel is needed to tell the arhiver where it have to extract file
                yield fullpath, filename

        # get paths for every submodule
        for submodule in Popen(
                "cd %s && git submodule --quiet foreach --recursive 'pwd'" %
            (repo_root, ),
                shell=True,
                stdout=PIPE).stdout.read().splitlines():
            #chdir(submodule)
            # in order to get output path we need to exclude repository path from the submodule path
            prefix = submodule[len(repo_root) + 1:]
            # recursion allows us to process repositories with more than one level of submodules
            #for git_file in git_files(path.join(repo_root,submodule)):
            for git_file in git_files(submodule, prefix=prefix):
                yield git_file

    if format == 'zip':
        from zipfile import ZipFile, ZIP_DEFLATED
        output_archive = ZipFile(path.abspath(output_file), 'w')
        for name, arcname in git_files():
            if verbose: print 'Compressing ' + arcname + '...'
            output_archive.write(name, prefix + arcname, ZIP_DEFLATED)
    elif format == 'tar':
        from tarfile import TarFile
        output_archive = TarFile(path.abspath(output_file), 'w')
        for name, arcname in git_files():
            if verbose: print 'Compressing ' + arcname + '...'
            #output_archive.add(path.join(name), arcname)
            #output_archive.add(path.join(name))
            output_archive.add(name, arcname)
Ejemplo n.º 8
0
    def create(self, output_path, dry_run=False, output_format=None):
        """
        Creates the archive, written to the given output_file_path

        Type of the archive is determined either by extension of output_file_path or by the format argument.
        Supported formats are: gz, zip, bz2, tar, tgz

        @type output_path:     string
        @param output_path:    Output file path.

        @type dry_run:  bool
        @param dry_run: Determines whether create should do nothing but print what it would archive.

        @type output_format:    string
        @param output_format:   Determines format of the output archive.
                                If None, format is determined from extension of output_file_path.
        """
        if output_format is None:
            file_name, file_ext = path.splitext(output_path)
            output_format = file_ext[len(extsep):].lower()

        if output_format == 'zip':
            from zipfile import ZipFile, ZIP_DEFLATED

            if not dry_run:
                archive = ZipFile(path.abspath(output_path), 'w')
                add = lambda file_path, file_name: archive.write(
                    file_path, path.join(self.prefix, file_name), ZIP_DEFLATED)
        elif output_format in ['tar', 'bz2', 'gz', 'tgz']:
            import tarfile

            if output_format == 'tar':
                t_mode = 'w'
            elif output_format == 'tgz':
                t_mode = 'w:gz'
            else:
                t_mode = 'w:{f}'.format(f=output_format)

            if not dry_run:
                archive = tarfile.open(path.abspath(output_path), t_mode)
                add = lambda file_path, file_name: archive.add(
                    file_path, path.join(self.prefix, file_name))
        else:
            raise RuntimeError("Unknown format: {f}".format(f=output_format))

        for file_path in self.extra:
            if not dry_run:
                if self.verbose:
                    print("Compressing {f} => {a}...".format(f=file_path,
                                                             a=path.join(
                                                                 self.prefix,
                                                                 file_path)))
                add(file_path, file_path)
            else:
                print("{f} => {a}".format(f=file_path,
                                          a=path.join(self.prefix, file_path)))

        for file_path in self.list_files():
            if not dry_run:
                if self.verbose:
                    print("Compressing {f} => {a}...".format(
                        f=path.join(self.main_repo_abspath, file_path),
                        a=path.join(self.prefix, file_path)))
                add(path.join(self.main_repo_abspath, file_path), file_path)
            else:
                print("{f} => {a}".format(f=path.join(self.main_repo_abspath,
                                                      file_path),
                                          a=path.join(self.prefix, file_path)))

        if not dry_run:
            archive.close()
Ejemplo n.º 9
0
def package(target, source, env):
    """Builder action for packaging the distribution archives."""

    # Print out.
    print('')
    print("#######################")
    print("# Packaging the files #")
    print("#######################")

    # List of distribution files.
    type_list = [env['DIST_TYPE']]
    if type_list[0] == 'ALL':
        type_list = ['zip', 'tar']

    # Loop over the distribution files.
    for dist_type in type_list:
        # The file name.
        if dist_type == 'zip':
            file = env['DIST_FILE'] + '.zip'
        elif dist_type == 'tar':
            file = env['DIST_FILE'] + '.tar.bz2'
        elif dist_type == 'dmg':
            file = env['DIST_FILE'] + '.dmg'

        # Print out.
        print("\n\nCreating the package distribution " + repr(file) + ".\n")

        # Create the special Mac OS X DMG file and then stop execution.
        if dist_type == 'dmg':
            # Create the Mac OS X universal application.
            print("\n# Creating the Mac OS X universal application.\n\n")
            cmd = '%s setup.py py2app' % sys.executable
            print("%s\n" % cmd)
            pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
            waitpid(pipe.pid, 0)

            # Create the dmg image.
            print("\n\n# Creating the DMG image.\n\n")
            cmd = 'hdiutil create -ov -fs HFS+ -volname "relax" -srcfolder dist/relax.app ../%s' % file
            print("%s\n" % cmd)
            pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
            waitpid(pipe.pid, 0)

            # Stop executing.
            return

        # Open the Zip distribution file.
        if dist_type == 'zip':
            archive = ZipFile(path.pardir + path.sep + file,
                              'w',
                              compression=8)

        # Open the Tar distribution file.
        elif dist_type == 'tar':
            if search('.bz2$', file):
                archive = TarFile.bz2open(path.pardir + path.sep + file, 'w')
            elif search('.gz$', file):
                archive = TarFile.gzopen(path.pardir + path.sep + file, 'w')
            else:
                archive = TarFile.open(path.pardir + path.sep + file, 'w')

        # Base directory.
        base = getcwd() + sep

        # Find all files untracked by the VC and not ignored.
        untracked = []
        if path.isdir(getcwd() + path.sep + ".git"):
            cmd = "git ls-files --others --exclude-standard"
            pipe = Popen(cmd, shell=True, stdout=PIPE, close_fds=False)
            for file_name in pipe.stdout.readlines():
                untracked.append(file_name.strip())

        # Walk through the directories.
        for root, dirs, files in walk(getcwd()):
            # Skip the version control directories.
            skip = False
            for vc in VERSION_CONTROL_DIRS:
                if search(vc, root):
                    skip = True
            if skip:
                continue

            # Add the files in the current directory to the archive.
            for i in range(len(files)):
                # Skip all blacklisted files.
                skip = False
                for file_name in BLACKLISTED_FILES:
                    if search(file_name, files[i]):
                        skip = True

                # Create the file name (without the base directory).
                name = path.join(root, files[i])
                name = name[len(base):]

                # Skip all untracked files.
                if name in untracked:
                    skip = True

                # Nothing to do.
                if skip:
                    continue

                # The archive file name.
                arcname = 'relax-' + version + path.sep + name
                print(arcname)

                # Zip archives.
                if dist_type == 'zip':
                    archive.write(filename=name, arcname=arcname)

                # Tar archives.
                if dist_type == 'tar':
                    archive.add(name=name, arcname=arcname)

        # Close the archive.
        archive.close()

    # Final printout.
    print("\n\n\n")
Ejemplo n.º 10
0
def archive_git(output_file,format='tar',repo_root='.', verbose=False):

    def git_files(repo_root=repo_root,exclude=True,verbose=False,prefix=''):

        excludes = []

        for filepath in Popen('cd %s && git ls-files --cached --full-name --no-empty-directory'%(repo_root,), shell=True, stdout=PIPE).stdout.read().splitlines():
            filename = path.join(filepath,prefix)
            fullpath = path.join(repo_root,filepath)


            # right now every time we find a .gitattributes, we
            # overwrite the previous patterns. might be nice to
            # use a dictionary that retains the patterns on a
            # parent-path level. But this works fine for a top-level
            # file in the main repo and top level of submodules
            if exclude and filename == '.gitattributes':
                excludes = []
                fh = open(filepath, 'r')
                for line in fh:
                    if not line: break
                    tokens = line.strip().split()
                    if 'export-ignore' in tokens[1:]:
                        excludes.append(tokens[0])
                fh.close()

            if not filename.startswith('.git') and not path.isdir(filepath):

                # check the patterns first
                ignore = False
                for pattern in excludes:
                    if fnmatch(filepath, pattern) or fnmatch(filename, pattern):
                        if verbose: print 'Exclude pattern matched (%s): %s' % (pattern, filepath)
                        ignore = True
                        break
                if ignore:
                    continue

                # baselevel is needed to tell the arhiver where it have to extract file
                yield fullpath,filename

        # get paths for every submodule
        for submodule in Popen("cd %s && git submodule --quiet foreach --recursive 'pwd'"%(repo_root,), shell=True, stdout=PIPE).stdout.read().splitlines():
            #chdir(submodule)
            # in order to get output path we need to exclude repository path from the submodule path
            prefix = submodule[len(repo_root)+1:]
            # recursion allows us to process repositories with more than one level of submodules
            #for git_file in git_files(path.join(repo_root,submodule)):
            for git_file in git_files(submodule,prefix=prefix):
                yield git_file


    if format == 'zip':
        from zipfile import ZipFile, ZIP_DEFLATED
        output_archive = ZipFile(path.abspath(output_file), 'w')
        for name, arcname in git_files():
            if verbose: print 'Compressing ' + arcname + '...'
            output_archive.write(name, prefix + arcname, ZIP_DEFLATED)
    elif format == 'tar':
        from tarfile import TarFile
        output_archive = TarFile(path.abspath(output_file), 'w')
        for name, arcname in git_files():
            if verbose: print 'Compressing ' + arcname + '...'
            #output_archive.add(path.join(name), arcname)
            #output_archive.add(path.join(name))
            output_archive.add(name,arcname)
Ejemplo n.º 11
0
def package(target, source, env):
    """Builder action for packaging the distribution archives."""

    # Print out.
    print('')
    print("#######################")
    print("# Packaging the files #")
    print("#######################")

    # List of distribution files.
    type_list = [env['DIST_TYPE']]
    if type_list[0] == 'ALL':
        type_list = ['zip', 'tar']

    # Loop over the distribution files.
    for dist_type in type_list:
        # The file name.
        if dist_type == 'zip':
            file = env['DIST_FILE'] + '.zip'
        elif dist_type == 'tar':
            file = env['DIST_FILE'] + '.tar.bz2'
        elif dist_type == 'dmg':
            file = env['DIST_FILE'] + '.dmg'

        # Print out.
        print("\n\nCreating the package distribution " + repr(file) + ".\n")

        # Create the special Mac OS X DMG file and then stop execution.
        if dist_type == 'dmg':
            # Create the Mac OS X universal application.
            print("\n# Creating the Mac OS X universal application.\n\n")
            cmd = '%s setup.py py2app' % sys.executable
            print("%s\n" % cmd)
            pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
            waitpid(pipe.pid, 0)

            # Create the dmg image.
            print("\n\n# Creating the DMG image.\n\n")
            cmd = 'hdiutil create -ov -fs HFS+ -volname "relax" -srcfolder dist/relax.app ../%s' % file
            print("%s\n" % cmd)
            pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
            waitpid(pipe.pid, 0)

            # Stop executing.
            return

        # Open the Zip distribution file.
        if dist_type == 'zip':
            archive = ZipFile(path.pardir + path.sep + file, 'w', compression=8)

        # Open the Tar distribution file.
        elif dist_type == 'tar':
            if search('.bz2$', file):
                archive = TarFile.bz2open(path.pardir + path.sep + file, 'w')
            elif search('.gz$', file):
                archive = TarFile.gzopen(path.pardir + path.sep + file, 'w')
            else:
                archive = TarFile.open(path.pardir + path.sep + file, 'w')

        # Base directory.
        base = getcwd() + sep

        # Walk through the directories.
        for root, dirs, files in walk(getcwd()):
            # Skip the subversion directories.
            if search("\.svn", root):
                continue

            # Add the files in the current directory to the archive.
            for i in range(len(files)):
                # Skip any '.sconsign' files, hidden files, byte-compiled '*.pyc' files, or binary objects '.o', '.os', 'obj', 'lib', and 'exp'.
                if search("\.sconsign", files[i]) or search("^\.", files[i]) or search("\.pyc$", files[i]) or search("\.o$", files[i]) or search("\.os$", files[i]) or search("\.obj$", files[i]) or search("\.lib$", files[i]) or search("\.exp$", files[i]):
                    continue

                # Create the file name (without the base directory).
                name = path.join(root, files[i])
                name = name[len(base):]
                print('relax-' + version + path.sep + name)

                # The archive file name.
                arcname = 'relax-' + version + path.sep + name

                # Zip archives.
                if dist_type == 'zip':
                    archive.write(filename=name, arcname=arcname)

                # Tar archives.
                if dist_type == 'tar':
                    archive.add(name=name, arcname=arcname)

        # Close the archive.
        archive.close()

    # Final printout.
    print("\n\n\n")
Ejemplo n.º 12
0
    def create(self, output_path, dry_run=False, output_format=None):
        """
        Create the archive at output_file_path.

        Type of the archive is determined either by extension of output_file_path or by output_format.
        Supported formats are: gz, zip, bz2, tar, tgz

        @param output_path: Output file path.
        @type output_path: string

        @param dry_run: Determines whether create should do nothing but print what it would archive.
        @type dry_run: bool

        @param output_format: Determines format of the output archive. If None, format is determined from extension
            of output_file_path.
        @type output_format: string
        """
        if output_format is None:
            file_name, file_ext = path.splitext(output_path)
            output_format = file_ext[len(extsep):].lower()
            self.LOG.debug(
                "Output format is not explicitly set, determined format is {0}."
                .format(output_format))

        if not dry_run:
            if output_format == 'zip':
                archive = ZipFile(path.abspath(output_path), 'w')

                def add_file(file_path, arcname):
                    if not path.islink(file_path):
                        archive.write(file_path, arcname, ZIP_DEFLATED)
                    else:
                        i = ZipInfo(arcname)
                        i.create_system = 3
                        i.external_attr = 0xA1ED0000
                        archive.writestr(i, readlink(file_path))
            elif output_format in ['tar', 'bz2', 'gz', 'tgz']:
                if output_format == 'tar':
                    t_mode = 'w'
                elif output_format == 'tgz':
                    t_mode = 'w:gz'
                else:
                    t_mode = 'w:{0}'.format(output_format)

                archive = tarfile.open(path.abspath(output_path), t_mode)
                add_file = lambda file_path, arcname: archive.add(
                    file_path, arcname)
            else:
                raise RuntimeError("Unknown format: {0}".format(output_format))

            def archiver(file_path, arcname):
                self.LOG.debug("Compressing {0} => {1}...".format(
                    file_path, arcname))
                add_file(file_path, arcname)
        else:
            archive = None
            archiver = lambda file_path, arcname: self.LOG.info(
                "{0} => {1}".format(file_path, arcname))

        self.archive_all_files(archiver)

        if archive is not None:
            archive.close()
Ejemplo n.º 13
0
def package(target, source, env):
    """Builder action for packaging the distribution archives."""

    # Print out.
    print('')
    print("#######################")
    print("# Packaging the files #")
    print("#######################")

    # List of distribution files.
    type_list = [env['DIST_TYPE']]
    if type_list[0] == 'ALL':
        type_list = ['zip', 'tar']

    # Loop over the distribution files.
    for dist_type in type_list:
        # The file name.
        if dist_type == 'zip':
            file = env['DIST_FILE'] + '.zip'
        elif dist_type == 'tar':
            file = env['DIST_FILE'] + '.tar.bz2'
        elif dist_type == 'dmg':
            file = env['DIST_FILE'] + '.dmg'

        # Print out.
        print("\n\nCreating the package distribution " + repr(file) + ".\n")

        # Create the special Mac OS X DMG file and then stop execution.
        if dist_type == 'dmg':
            # Create the Mac OS X universal application.
            print("\n# Creating the Mac OS X universal application.\n\n")
            cmd = '%s setup.py py2app' % sys.executable
            print("%s\n" % cmd)
            pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
            waitpid(pipe.pid, 0)

            # Create the dmg image.
            print("\n\n# Creating the DMG image.\n\n")
            cmd = 'hdiutil create -ov -fs HFS+ -volname "relax" -srcfolder dist/relax.app ../%s' % file
            print("%s\n" % cmd)
            pipe = Popen(cmd, shell=True, stdin=PIPE, close_fds=False)
            waitpid(pipe.pid, 0)

            # Stop executing.
            return

        # Open the Zip distribution file.
        if dist_type == 'zip':
            archive = ZipFile(path.pardir + path.sep + file,
                              'w',
                              compression=8)

        # Open the Tar distribution file.
        elif dist_type == 'tar':
            if search('.bz2$', file):
                archive = TarFile.bz2open(path.pardir + path.sep + file, 'w')
            elif search('.gz$', file):
                archive = TarFile.gzopen(path.pardir + path.sep + file, 'w')
            else:
                archive = TarFile.open(path.pardir + path.sep + file, 'w')

        # Base directory.
        base = getcwd() + sep

        # Walk through the directories.
        for root, dirs, files in walk(getcwd()):
            # Skip the subversion directories.
            if search("\.svn", root):
                continue

            # Add the files in the current directory to the archive.
            for i in range(len(files)):
                # Skip any '.sconsign' files, hidden files, byte-compiled '*.pyc' files, or binary objects '.o', '.os', 'obj', 'lib', and 'exp'.
                if search("\.sconsign",
                          files[i]) or search("^\.", files[i]) or search(
                              "\.pyc$", files[i]) or search(
                                  "\.o$", files[i]) or search(
                                      "\.os$", files[i]) or search(
                                          "\.obj$", files[i]) or search(
                                              "\.lib$", files[i]) or search(
                                                  "\.exp$", files[i]):
                    continue

                # Create the file name (without the base directory).
                name = path.join(root, files[i])
                name = name[len(base):]
                print('relax-' + version + path.sep + name)

                # The archive file name.
                arcname = 'relax-' + version + path.sep + name

                # Zip archives.
                if dist_type == 'zip':
                    archive.write(filename=name, arcname=arcname)

                # Tar archives.
                if dist_type == 'tar':
                    archive.add(name=name, arcname=arcname)

        # Close the archive.
        archive.close()

    # Final printout.
    print("\n\n\n")