<commit_before>#!/usr/bin/env python
''' Script to make a copy of pynifti / brifti for insertion into NIPY tree

It downloads a copy from the repository, patches the paths, tests, makes
an archive, deletes temporary copies, and exits.

Unix specific.  It could be generalized, but it's only for occasional
use in updating the code tree, and probably won't be used for long
either.

Typical use would be:

copy_brifti.py /tmp/brifti-0.2.tar.gz
cd ~/nipy-repo/trunk-lp/nipy/io
tar zcvf /tmp/brifti-0.2.tar.gz

then bzr stat, commit as necessary
'''

import os
from os.path import join as pjoin
import sys
import shutil
import tempfile
import functools
from subprocess import call
import re

import nose

# search replaces for imports
subs = (
    (re.compile(r'^([ >]*)(import|from) +nifti'),
     r'\1\2 nipy.io.imageformats'),
    )

caller = functools.partial(call, shell=True)
git_path = 'git://git.debian.org/git/pkg-exppsy/pynifti.git'
# because I am working locally for the moment
#git_path = '/home/mb312/dev_trees/pynifti/.git'


def create_archive(out_path, git_path, git_id):
    out_path = os.path.abspath(out_path)
    pwd = os.path.abspath(os.curdir)
    # pull out git archive
    tmp_path = tempfile.mkdtemp()
    os.chdir(tmp_path)
    caller('git clone ' + git_path)
    # extract nifti library tree from git archive
    os.chdir('pynifti')
    caller('git archive %s nifti > nifti.tar' % git_id)
    os.chdir(tmp_path)
    caller('tar xvf pynifti/nifti.tar')
    shutil.rmtree('pynifti')
    # create path structure for moved imports
    os.makedirs(pjoin('nipy', 'io'))
    file(pjoin('nipy', '__init__.py'), 'wt').write('\n')
    file(pjoin('nipy', 'io', '__init__.py'), 'wt').write('\n')
    os.rename('nifti', pjoin('nipy','io','imageformats'))
    # do search and replace for imports to change to NIPY ones
    for root, dirs, files in os.walk('nipy'):
        for fname in files:
            if not fname.endswith('.py'):
                continue
            fpath = os.path.join(root, fname)
            lines = file(fpath).readlines()
            outfile = file(fpath, 'wt')
            for line in lines:
                for regexp, repstr in subs:<commit_msg>Some doc/comment changes to copy_brifti.py script<commit_after>#!/usr/bin/env python
''' Script to make a copy of pynifti / brifti for insertion into NIPY tree

It downloads a copy from the repository, patches the paths, tests, makes
an archive, deletes temporary copies, and exits.

Unix specific.  It could be generalized, but it's only for occasional
use in updating the code tree, and probably won't be used for long
either.

Typical use would be (to port over changes for revision tagged in git as
'brifti-0.4':

copy_brifti.py /tmp/brifti-0.4.tar.gz brifti-0.4
cd ~/nipy-repo/trunk-lp/nipy/io
tar zcvf /tmp/brifti-0.4.tar.gz

then bzr stat, commit as necessary.

Or, when working locally, something like:

copy_brifti.py /tmp/brifti-0.4.tar.gz brifti-0.4 ~/dev_trees/pynifti/.git

to replace the first line above.
'''

import os
from os.path import join as pjoin
import sys
import shutil
import tempfile
import functools
from subprocess import call
import re

import nose

# search replaces for imports
subs = (
    (re.compile(r'^([ >]*)(import|from) +nifti'),
     r'\1\2 nipy.io.imageformats'),
    )

caller = functools.partial(call, shell=True)
git_path = 'git://git.debian.org/git/pkg-exppsy/pynifti.git'


def create_archive(out_path, git_path, git_id):
    out_path = os.path.abspath(out_path)
    pwd = os.path.abspath(os.curdir)
    # pull out git archive
    tmp_path = tempfile.mkdtemp()
    os.chdir(tmp_path)
    caller('git clone ' + git_path)
    # extract nifti library tree from git archive
    os.chdir('pynifti')
    caller('git archive %s nifti > nifti.tar' % git_id)
    os.chdir(tmp_path)
    caller('tar xvf pynifti/nifti.tar')
    shutil.rmtree('pynifti')
    # create path structure for moved imports
    os.makedirs(pjoin('nipy', 'io'))
    file(pjoin('nipy', '__init__.py'), 'wt').write('\n')
    file(pjoin('nipy', 'io', '__init__.py'), 'wt').write('\n')
    os.rename('nifti', pjoin('nipy','io','imageformats'))
    # do search and replace for imports to change to NIPY ones
    for root, dirs, files in os.walk('nipy'):
        for fname in files:
            if not fname.endswith('.py'):
                continue
            fpath = os.path.join(root, fname)
            lines = file(fpath).readlines()
            outfile = file(fpath, 'wt')
            for line in lines:
                for regexp, repstr in subs:
{'fraction_non_alphanumeric': 0.12395420519594892, 'fraction_numerical': 0.005063848524878908, 'mean_word_length': 3.198706099815157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3908719', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1602, 'n_words': 464}