Create marfile from the contents of srcdir
500783
def packmar(marfile, srcdir):
    """"""
    nullfd = open(os.devnull, "w")
    files = [f[len(srcdir) + 1:] for f in findfiles(srcdir)]
    marfile = cygpath(os.path.abspath(marfile))
    try:
        check_call(
            [MAR, '-c', marfile] + files, cwd=srcdir, preexec_fn=_noumask)
    except Exception:
        log.exception("Error packing mar file %s from %s", marfile, srcdir)
        raise
    nullfd.close()
585919
function copyTemplateDir (srcDir, outDir, vars, cb) {
  if (!cb) {
    if (!vars) vars = noop
    cb = vars
    vars = {}
  }

  assert.equal(typeof srcDir, 'string')
  assert.equal(typeof outDir, 'string')
  assert.equal(typeof vars, 'object')
  assert.equal(typeof cb, 'function')

  // create directory
  mkdirp(outDir, function (err) {
    if (err) return cb(err)

    const rs = readdirp({ root: srcDir })
    const streams = []
    const createdFiles = []

    // create a new stream for every file emitted
    rs.on('data', function (file) {
      createdFiles.push(path.join(outDir, maxstache(removeUnderscore(file.path), vars)))
      streams.push(writeFile(outDir, vars, file))
    })

    // delegate errors & close streams
    eos(rs, function (err) {
      if (err) return cb(err)
      parallel(streams, function (err) {
        if (err) return cb(err)
        cb(null, createdFiles)
      })
    })
  })
}
72230
def build_readme(base_path=None):
    """"""
    if base_path:
        path = os.path.join(base_path, 'README.md')
    else:
        path = 'README.md'
    convert_md_to_rst(path)
    print("Successfully converted README.md to README.rst")
1015326
def packtar(tarfile, files, srcdir):
    
    nullfd = open(os.devnull, "w")
    tarfile = cygpath(os.path.abspath(tarfile))
    log.debug("pack tar %s from folder  %s with files ", tarfile, srcdir)
    log.debug(files)
    try:
        check_call([TAR, '-czf', tarfile] + files, cwd=srcdir,
                   stdout=nullfd, preexec_fn=_noumask)
    except Exception:
        log.exception("Error packing tar file %s to %s", tarfile, srcdir)
        raise
    nullfd.close()
959053
def unpackfile(filename, destdir):
    """"""
    if filename.endswith(".mar"):
        return unpackmar(filename, destdir)
    elif filename.endswith(".exe"):
        return unpackexe(filename, destdir)
    elif filename.endswith(".tar") or filename.endswith(".tar.gz") \
            or filename.endswith(".tgz"):
        return unpacktar(filename, destdir)
    else:
        raise ValueError("Unknown file type: %s" % filename)
508659
def rrmdir(directory):
    
    for root, dirs, files in os.walk(directory, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))
    os.rmdir(directory)
1106185
def build_src(ctx, dest=None):
    
    if dest:
        if not dest.startswith('/'):
            # Relative
            dest = os.path.join(os.getcwd(), dest)

        os.chdir(PROJECT_DIR)
        ctx.run('python setup.py sdist --dist-dir {0}'.format(dest))
    else:
        os.chdir(PROJECT_DIR)
        ctx.run('python setup.py sdist')
506425
def tar_dir(tarfile, srcdir):
    
    files = os.listdir(srcdir)
    packtar(tarfile, files, srcdir)
1177062
def from_file(cls, file_path, compressed=False, encoded=False):
        """"""
        file_id = '.'.join(path.basename(file_path).split('.')[:-1])
        file_format = file_path.split('.')[-1]
        content = cls(file_id, file_format, compressed, encoded)
        content.file_exists = True
        content._location = path.dirname(file_path)
        return content
794535
def recursive_copy(src_dir, dest_dir):
  

  file_io.recursive_create_dir(dest_dir)
  for file_name in file_io.list_directory(src_dir):
    old_path = os.path.join(src_dir, file_name)
    new_path = os.path.join(dest_dir, file_name)

    if file_io.is_directory(old_path):
      recursive_copy(old_path, new_path)
    else:
      file_io.copy(old_path, new_path, overwrite=True)
140180
function (destdir, srcdir, srcfile, defines, txt) {
            /*  expand variables  */
            if (options.expandDefines) {
                txt = txt.replace(options.directiveSyntax.expand,
                    function (directive, name) {

                    /*  skip special case  */
                    if (name === "include")
                        return directive;

                    /*  resolve variable  */
                    var txt = defines[name];
                    if (typeof txt === "undefined") {
                        if (options.onUndefinedVariable === "keep")
                            txt = directive;
                        else if (options.onUndefinedVariable === "empty")
                            txt = "";
                        else /* if (options.onUndefinedVariable === "error") */
                            throw "Variable \"" + name + "\" not defined.";
                    }
                    return txt;
                });
            }

            /*  expand includes  */
            if (options.expandIncludes) {
                txt = txt.replace(options.directiveSyntax.include,
                    function (directive, prolog, _q, file, definitions, epilog) {

                    /*  resolve possibly existing escapes  */
                    file = file.replace(new RegExp("\\\\" + _q, "g"), _q);

                    /*  process file  */
                    if (!grunt.file.isPathAbsolute(file))
                        file = path.resolve(path.join(srcdir, file));
                    if (!grunt.file.exists(file))
                        throw "Include file \"" + file + "\" not found.";
                    var include_dir = path.dirname(file);
                    var include_file = path.basename(file);
                    var txt = grunt.file.read(file);

                    /*  optionally strip header comment of includes  */
                    if (options.stripHeaderOfInclude)
                        txt = txt.replace(options.directiveSyntax.header, "");

                    /*  process defines  */
                    var include_defines = defines;
                    if (typeof definitions !== "undefined" && definitions !== "") {
                        definitions.replace(options.directiveSyntax.define,
                            function (define, _q1, name, _q2, value) {
                            /*  resolve possibly existing escapes  */
                            if (typeof _q1 !== "undefined" && _q1 !== "")
                                value = value.replace(new RegExp("\\\\" + _q1, "g"), _q1);
                            value = value.replace(new RegExp("\\\\" + _q2, "g"), _q2);

                            /*  store definition  */
                            include_defines[name] = value;
                        });
                    }

                    /*  expand included text (RECURSION!)  */
                    txt = expandText(
                        destdir,
                        include_dir,
                        include_file,
                        include_defines,
                        txt
                    );

                    /*  post whitespace processing  */
                    if (options.repeatWhitespaceProlog)
                        txt = txt.replace(/^(?=.)/mg, prolog);
                    if (options.keepWhitespaceProlog)
                        txt = prolog + txt;
                    if (options.keepWhitespaceEpilog)
                        txt = txt + epilog;
                    return txt;
                });
            }

            /*  adjust references  */
            if (options.adjustReferences) {
                txt = txt.replace(options.directiveSyntax.adjust,
                    function (directive, prolog, _q, ref, epilog) {

                    /*  resolve possibly existing escapes  */
                    ref = ref.replace(new RegExp("\\\\" + _q, "g"), _q);

                    /*  only adjust references which can be resolved on the filesystem  */
                    var file = path.join(srcdir, ref);
                    if (grunt.file.exists(file)) {
                        /*  adjust path from source to destination anchor  */
                        var ref_new = path.normalize(path.join(path.relative(destdir, srcdir), ref));

                        /*  ensure that the reference is still correct based on the destination  */
                        file = path.join(destdir, ref_new);
                        if (!grunt.file.exists(file))
                            throw "Adjusted referenced file \"" + ref_new + "\" not found " +
                                "(from destination directory \"" + destdir + "\").";

                        /*  provide adjusted directive  */
                        directive = prolog + ref_new + epilog;
                    }

                    return directive;
                });
            }

            return txt;
        }
1054114
def _copy(src, dst):
    """"""
    try:
        shutil.copy(src, dst)
    except IsADirectoryError:
        if os.path.isdir(dst):
            dst = os.path.join(dst, os.path.basename(src))
        shutil.copytree(src, dst)
495585
def load_conf(srcdir):
    
    path = os.path.join(srcdir, 'conf.py')
    mylocals = {'__file__': path}
    with open(path) as fd:
        exec(fd.read(), mylocals)
    return mylocals
11586
def create_from(cls, src_path):
        
        is_dir = isdir(src_path)
        target_files, size_data = cls._get_target_files_info(src_path)

        SIZE_MIN = 32768  # 32 KiB
        SIZE_DEFAULT = 262144  # 256 KiB
        SIZE_MAX = 1048576  # 1 MiB

        CHUNKS_MIN = 1000  # todo use those limits as advised
        CHUNKS_MAX = 2200

        size_piece = SIZE_MIN
        if size_data > SIZE_MIN:
            size_piece = SIZE_DEFAULT

        if size_piece > SIZE_MAX:
            size_piece = SIZE_MAX

        def read(filepath):
            with open(filepath, 'rb') as f:
                while True:
                    chunk = f.read(size_piece - len(pieces_buffer))
                    chunk_size = len(chunk)
                    if chunk_size == 0:
                        break
                    yield chunk

        pieces = bytearray()
        pieces_buffer = bytearray()

        for fpath, _, _ in target_files:
            for chunk in read(fpath):
                pieces_buffer += chunk

                if len(pieces_buffer) == size_piece:
                    pieces += sha1(pieces_buffer).digest()[:20]
                    pieces_buffer = bytearray()

        if len(pieces_buffer):
            pieces += sha1(pieces_buffer).digest()[:20]
            pieces_buffer = bytearray()

        info = {
            'name': basename(src_path),
            'pieces': bytes(pieces),
            'piece length': size_piece,
        }

        if is_dir:
            files = []

            for _, length, path in target_files:
                files.append({'length': length, 'path': path})

            info['files'] = files

        else:
            info['length'] = target_files[0][1]

        torrent = cls({'info': info})
        torrent.created_by = get_app_version()
        torrent.creation_date = datetime.utcnow()

        return torrent
782190
def gen_ascii_docs(src='fastai'):
    
    os.chdir(Path(__file__).absolute().parent)
    with working_directory('..'):
        path = Path(src)
        if path.is_dir():
            file_paths = list(path.glob('**/*.py'))
        else:
            file_paths = [path]

    pat = re.compile('^(?!__init__).*.py\Z')
    for file_path in file_paths:
        if pat.match(file_path.name):
            file_path.parent.mkdir(parents=True, exist_ok=True)
            with working_directory('..'):
                tmpl_str = parse_module(file_path)

            (file_path.parent/(file_path.name.rsplit('.',1)[0] + '.adoc.tmpl')).write_text(tmpl_str)
            (file_path.parent/(file_path.name.rsplit('.',1)[0] + '.adoc')).write_text(re.sub(r"{{(.*?)}}", parse_tmpl, tmpl_str, flags=re.DOTALL))
    if path.is_dir():
        subprocess.call(['asciidoctor', str(path) + '/**/*.adoc'])
    else:
        subprocess.call(['asciidoctor', str(path).rsplit('.',1)[0] + '.adoc'])
489112
def createCorrFile(outfile, arrlist, template):
    
    # Remove the existing cor file if it exists
    if os.path.isfile(outfile):
        os.remove(outfile)
        print("Removing old corr file: '{:s}'".format(outfile))

    with fits.open(template, memmap=False) as ftemplate:
        for arr in arrlist:
            ftemplate[arr['sciext']].data = arr['corrFile']
            if arr['dqext'][0] != arr['sciext'][0]:
                ftemplate[arr['dqext']].data = arr['dqMask']
        ftemplate.writeto(outfile)
        print("Created CR corrected file: '{:s}'".format(outfile))
1251643
def saveHdlFiles(self, srcDir):
        
        path = os.path.join(srcDir, self.name)
        try:
            os.makedirs(path)
        except OSError:
            # wipe if exists
            shutil.rmtree(path)
            os.makedirs(path)

        files = self.hdlFiles
        self.hdlFiles = self.toHdlConversion(self.top, self.name, path)
        for srcF in files:
            dst = os.path.join(path,
                               os.path.relpath(srcF, srcDir).replace('../', '')
                               )
            os.makedirs(os.path.dirname(dst), exist_ok=True)
            shutil.copy(srcF, dst)
            self.hdlFiles.append(dst)