@@ -49,6 +49,10 @@ from os.path import join as pjoin
49
49
50
50
from random import sample
51
51
52
+ # Minimal versions of external dependencies
53
+ MIN_VERSIONS = {
54
+ 'datalad' : '0.7'
55
+ }
52
56
PY3 = sys .version_info [0 ] >= 3
53
57
54
58
import logging
@@ -1024,11 +1028,13 @@ def add_rows_to_scans_keys_file(fn, newrows):
1024
1028
1025
1029
header = ['filename' , 'acq_time' , 'operator' , 'randstr' ]
1026
1030
# save
1031
+ # prepare all the data rows
1032
+ data_rows = [[k ] + v for k , v in fnames2info .items ()]
1033
+ # sort by the date/filename
1034
+ data_rows_sorted = sorted (data_rows , key = lambda x : (x [1 ], x [0 ]))
1027
1035
with open (fn , 'a' ) as csvfile :
1028
1036
writer = csv .writer (csvfile , delimiter = '\t ' )
1029
- writer .writerow (header )
1030
- for key in sorted (fnames2info .keys ()):
1031
- writer .writerow ([key ] + fnames2info [key ])
1037
+ writer .writerows ([header ] + data_rows_sorted )
1032
1038
1033
1039
1034
1040
def _find_subj_ses (f_name ):
@@ -1561,6 +1567,9 @@ def create_file_if_missing(filename, content):
1561
1567
"""Create file if missing, so we do not override any possibly introduced changes"""
1562
1568
if exists (filename ):
1563
1569
return False
1570
+ dirname = os .path .dirname (filename )
1571
+ if not os .path .exists (dirname ):
1572
+ os .makedirs (dirname )
1564
1573
with open (filename , 'w' ) as f :
1565
1574
f .write (content )
1566
1575
return True
@@ -1697,7 +1706,7 @@ def mark_sensitive(ds, path_glob=None):
1697
1706
if not paths :
1698
1707
return
1699
1708
sens_kwargs ['path' ] = paths
1700
- ds .metadata (** sens_kwargs )
1709
+ ds .metadata (recursive = True , ** sens_kwargs )
1701
1710
1702
1711
1703
1712
def add_to_datalad (topdir , studydir , msg = None , bids = False ):
@@ -1708,7 +1717,8 @@ def add_to_datalad(topdir, studydir, msg=None, bids=False):
1708
1717
from datalad .support .annexrepo import AnnexRepo
1709
1718
1710
1719
from datalad .support .external_versions import external_versions
1711
- assert external_versions ['datalad' ] >= '0.5.1' , "Need datalad >= 0.5.1"
1720
+ # 0.7 added .metadata
1721
+ assert external_versions ['datalad' ] >= MIN_VERSIONS ['datalad' ], "Need datalad >= 0.7"
1712
1722
1713
1723
studyrelpath = os .path .relpath (studydir , topdir )
1714
1724
assert not studyrelpath .startswith (os .path .pardir ) # so we are under
@@ -1753,21 +1763,37 @@ def add_to_datalad(topdir, studydir, msg=None, bids=False):
1753
1763
ds = Dataset (studydir )
1754
1764
# Add doesn't have all the options of save such as msg and supers
1755
1765
ds .add ('.gitattributes' , to_git = True , save = False )
1756
- dsh = None
1766
+ dsh = dsh_path = None
1757
1767
if os .path .lexists (os .path .join (ds .path , '.heudiconv' )):
1758
- dsh = Dataset (opj (ds .path , '.heudiconv' ))
1768
+ dsh_path = opj (ds .path , '.heudiconv' )
1769
+ dsh = Dataset (dsh_path )
1759
1770
if not dsh .is_installed ():
1760
- # we need to create it first
1761
- dsh = ds .create (path = '.heudiconv' ,
1762
- force = True ,
1763
- shared_access = 'all' )
1771
+ # Previously we did not have it as a submodule, and since no
1772
+ # automagic migration is implemented, we just need to check first
1773
+ # if any path under .heudiconv is already under git control
1774
+ if any (x [0 ].startswith ('.heudiconv/' ) for x in
1775
+ ds .repo .repo .index .entries .keys ()):
1776
+ lgr .warning (
1777
+ '%s has .heudiconv not as a submodule from previous versions '
1778
+ 'of heudiconv. No automagic migration is yet provided' , ds
1779
+ )
1780
+ else :
1781
+ # use/create a submodule dataset for .heudiconv
1782
+ dsh = ds .create (path = '.heudiconv' ,
1783
+ force = True ,
1784
+ shared_access = 'all' )
1764
1785
# Since .heudiconv could contain sensitive information
1765
1786
# we place all files under annex and then add
1766
1787
if create_file_if_missing (
1767
- opj (dsh . path , '.gitattributes' ),
1788
+ opj (dsh_path , '.gitattributes' ),
1768
1789
"""* annex.largefiles=anything
1769
1790
""" ):
1770
- dsh .add ('.gitattributes' , message = "Added gitattributes to place all content under annex" )
1791
+ # should work properly if .heudiconv is a submodule or not
1792
+ ds .add (
1793
+ '.heudiconv/.gitattributes' ,
1794
+ to_git = True ,
1795
+ message = "Added gitattributes to place all .heudiconv content under annex"
1796
+ )
1771
1797
ds .add ('.' , recursive = True , save = False ,
1772
1798
# not in effect! ?
1773
1799
#annex_add_opts=['--include-dotfiles']
@@ -1781,9 +1807,10 @@ def add_to_datalad(topdir, studydir, msg=None, bids=False):
1781
1807
mark_sensitive (ds , '*/*/*_scans.tsv' ) # within sess/subj
1782
1808
mark_sensitive (ds , '*/anat' ) # within subj
1783
1809
mark_sensitive (ds , '*/*/anat' ) # within ses/subj
1784
- if dsh :
1785
- mark_sensitive (dsh ) # entire .heudiconv!
1786
- dsh .save (message = msg )
1810
+ if dsh_path :
1811
+ mark_sensitive (ds , '.heudiconv' ) # entire .heudiconv!
1812
+ # if dsh and dsh.is_installed():
1813
+ # dsh.save(message=msg)
1787
1814
ds .save (message = msg , recursive = True , super_datasets = True )
1788
1815
1789
1816
assert not ds .repo .dirty
0 commit comments