<commit_before>#!/usr/bin/tclsh
#
# $Id$
#
# Upgrade sources.conf for a given prefix (passed as the first and only
# argument).
#
# For an rsync: repository, if it is the standard MacPorts one and not
# already tagged, then make it default, if another wasn't already default.
# For a file:// respository, if it is an svn checkout from the MacPorts
# server, then make it default if another hasn't already been tagged.
#

if {[llength $::argv] == 0} {
   puts "Usage: ${::argv0} <prefix>"
   exit 1
}

set prefix [lindex $::argv 0]
set sourcesConf ${prefix}/etc/macports/sources.conf
if {[catch {set sourcesConfChannel [open $sourcesConf r]}]} {
   exit 0
}

set mktempChannel [open "|/usr/bin/mktemp -t macports_sources_upgrade" r]
set tempfile [read -nonewline $mktempChannel]
close $mktempChannel

set tempfileChannel [open $tempfile w]
set defaultSeen false
set defaultWritten false

while {[gets $sourcesConfChannel line] >= 0} {
   if {!$defaultSeen && ![regexp {^\s*#|^$} $line]} {
      if {[string first {[default]} $line] >= 0} {
         set defaultSeen true
      } elseif {[regexp {^\s*rsync://rsync\.macports\.org/release/ports/} $line]} {
         set line "$line \[default\]"
         set defaultSeen true
         set defaultWritten true
      } elseif {[regexp {^\s*file://(/[^ #]+)} $line -> filepath]} {
         if {[file exists [file join ${filepath} .svn]]} {
            set svnChannel [open "|svn info ${filepath}" r]
            set svnURL {}
            while {[gets $svnChannel svnLine] >= 0} {
               regexp {^URL: (.*)} $svnLine -> svnURL
            }
            close $svnChannel
            if {[regexp {^https?://svn\.macports\.org/repository/macports/trunk/dports} $svnURL]} {
               set line "${line} \[default\]"
               set defaultSeen true
               set defaultWritten true
            }
         }
      }
   }
   puts $tempfileChannel $line
}
close $tempfileChannel
close $sourcesConfChannel

if {$defaultWritten} {
   set attributes [file attributes ${sourcesConf}]
   file rename ${sourcesConf} "${sourcesConf}.mpsaved"
   file rename ${tempfile} ${sourcesConf}
   eval file attributes ${sourcesConf} $attributes
}
exit 0
<commit_msg>Cope with the case where sources.conf.mpsaved already exists.<commit_after>#!/usr/bin/tclsh
#
# $Id$
#
# Upgrade sources.conf for a given prefix (passed as the first and only
# argument).
#
# For an rsync: repository, if it is the standard MacPorts one and not
# already tagged, then make it default, if another wasn't already default.
# For a file:// respository, if it is an svn checkout from the MacPorts
# server, then make it default if another hasn't already been tagged.
#

if {[llength $::argv] == 0} {
   puts "Usage: ${::argv0} <prefix>"
   exit 1
}

set prefix [lindex $::argv 0]
set sourcesConf ${prefix}/etc/macports/sources.conf
if {[catch {set sourcesConfChannel [open $sourcesConf r]}]} {
   exit 0
}

set mktempChannel [open "|/usr/bin/mktemp -t macports_sources_upgrade" r]
set tempfile [read -nonewline $mktempChannel]
close $mktempChannel

set tempfileChannel [open $tempfile w]
set defaultSeen false
set defaultWritten false

while {[gets $sourcesConfChannel line] >= 0} {
   if {!$defaultSeen && ![regexp {^\s*#|^$} $line]} {
      if {[string first {[default]} $line] >= 0} {
         set defaultSeen true
      } elseif {[regexp {^\s*rsync://rsync\.macports\.org/release/ports/} $line]} {
         set line "$line \[default\]"
         set defaultSeen true
         set defaultWritten true
      } elseif {[regexp {^\s*file://(/[^ #]+)} $line -> filepath]} {
         if {[file exists [file join ${filepath} .svn]]} {
            set svnChannel [open "|svn info ${filepath}" r]
            set svnURL {}
            while {[gets $svnChannel svnLine] >= 0} {
               regexp {^URL: (.*)} $svnLine -> svnURL
            }
            close $svnChannel
            if {[regexp {^https?://svn\.macports\.org/repository/macports/trunk/dports} $svnURL]} {
               set line "${line} \[default\]"
               set defaultSeen true
               set defaultWritten true
            }
         }
      }
   }
   puts $tempfileChannel $line
}
close $tempfileChannel
close $sourcesConfChannel

if {$defaultWritten} {
   set attributes [file attributes ${sourcesConf}]
   if {[catch {file rename ${sourcesConf} "${sourcesConf}.mpsaved"}]} {
      file rename -force ${sourcesConf} "${sourcesConf}.mpsaved_[clock seconds]"
   }
   file rename ${tempfile} ${sourcesConf}
   eval file attributes ${sourcesConf} $attributes
}
exit 0

{'fraction_non_alphanumeric': 0.13790835343126506, 'fraction_numerical': 0.0039465029598772195, 'mean_word_length': 2.8530405405405403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5308236', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1427, 'n_words': 461}