@tyhld/conductor 0.12.137 → 0.12.139

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -662,6 +662,23 @@ def tokens_of(segment: str):
662
662
  return segment.split()
663
663
 
664
664
 
665
+ def naive_token_split(segment) -> bool:
666
+ """この区切りの語の割り方が【素朴分割へ倒れた】か(便 362a7759)。
667
+
668
+ ★tokens_of と同じ判定を同じ道具で行う(=1か所で決める)。倒れたときだけ、
669
+ 語に引用符が残る=1つの探す言葉が2語以上に割れていることがある。
670
+ 倒れていないとき(shlex が通ったとき)は引用符が剥がされているので、
671
+ 探す言葉は必ず1語で閉じている。
672
+ """
673
+ if not segment:
674
+ return False
675
+ try:
676
+ shlex.split(segment, comments=False, posix=True)
677
+ except ValueError:
678
+ return True
679
+ return False
680
+
681
+
665
682
  # 先頭に付く環境設定・ラッパ(実体の動詞まで読み進める)。
666
683
  _WRAPPERS = frozenset({
667
684
  'env', 'nohup', 'time', 'stdbuf', 'setsid', 'timeout', 'nice', 'ionice',
@@ -887,7 +904,7 @@ _PATTERN_FLAGS = ('-e', '--regexp')
887
904
  _PATTERN_FILE_FLAGS = ('-f', '--file')
888
905
 
889
906
 
890
- def _pattern_word_positions(args, first: int, word=None):
907
+ def _pattern_word_positions(args, first: int, word=None, naive: bool = False):
891
908
  """探す文字の語の位置を返す(引用符が壊れて空白で割られた続きも含める)。
892
909
 
893
910
  ★word を渡せば、引用符の開き具合をその字面で見る(`--regexp=<探す文字>` のように
@@ -903,21 +920,27 @@ def _pattern_word_positions(args, first: int, word=None):
903
920
  パスとして測られ、`.env` で終わって見える語が秘密のファイルに当たって
904
921
  読むだけの検索が danger で止まっていた(実地 便 3f54ac4a で便が丸ごと止まった)。
905
922
 
906
- 【どう使うか】最初の語が【引用符そのもので始まり、しかもその引用符が閉じていない】ときだけ、
907
- その引用符が閉じるまでの語を同じ探す文字の続きとして外す。
908
- ★「引用符で始まる」を条件にするのが要点。語の割り方が正しいとき(shlex が通ったとき)は
909
- 引用符が剥がされているので、この条件には当たらない=続きを外さない。
910
- これが無いと、探す言葉の【途中】に引用符が1つ入る形(`-rn "it's" <フォルダ>`)で
911
- 名指しのフォルダまで探す文字と見なし、作業場所に倒れてしまう(この便の測りで実測)。
923
+ 【どう使うか(便 362a7759 で「割れたか」の見分け方を根本から直した)】
924
+ 語の割り方が【素朴分割へ倒れた】ときだけ、開いたままの引用符が閉じるまでの語を
925
+ 同じ探す文字の続きとして外す(naive=True)。倒れていないとき(shlex が通ったとき)は
926
+ 引用符が剥がされていて探す文字は必ず1語で閉じているので、続きを1つも外さない。
927
+ ★見分けるのは「倒れたかどうか」であって「引用符で始まるかどうか」ではない。
928
+ 便 18e9e32e は「最初の語が引用符そのもので始まる」を条件にしていたが、これは
929
+ ① `$'…'` のように引用符で始まらない書き方(実測 この便の形A)では続きを外せず、
930
+ 割れた2語目がパスとして測られて読むだけの検索が danger で止まり、
931
+ ② 逆に、割れていないのに語の【途中】に引用符が1つ在る形(`-n "'" <秘密のファイル>`)では
932
+ 続きを外してしまい、名指しの秘密のファイルまで探す文字として読み飛ばしていた
933
+ (実測 この便の重大発見2=止める側の穴)。
934
+ 「倒れたかどうか」で見ると①②が同時に正しくなる(探す言葉の【途中】に引用符が1つ入る形
935
+ `-rn "it's" <フォルダ>` は shlex が通る=倒れていないので、今までどおり続きを外さない)。
912
936
  ★止める側は1件も減らない:クォートが壊れた切れ端は、同じ命令を本物の区切り
913
937
  (split_commands・引用符を守る割り方)でも必ず測り直す(_real_segments・便 2.5)ので、
914
938
  `<検索の道具> -n 'a\\|b' <秘密のファイル>` のような形は、そちらで今までどおり止まる。
915
- ★引用符が壊れていない(=語の割り方が正しい)ときは、最初の1語だけを外す
916
- =これまでと同じ振る舞い。
917
939
  """
918
940
  out = [first]
919
941
  word = args[first] if word is None else word
920
- if word[:1] not in ('"', "'"):
942
+ if not naive:
943
+ # 語の割り方が正しかった=引用符は剥がされている。探す文字はこの1語で閉じている。
921
944
  return out
922
945
  quote = _open_quote(word)
923
946
  i = first + 1
@@ -928,14 +951,18 @@ def _pattern_word_positions(args, first: int, word=None):
928
951
  return out
929
952
 
930
953
 
931
- def pattern_arg_positions(verb: str, args):
954
+ def pattern_arg_positions(verb: str, args, seg=None):
932
955
  """探す文字として扱う引数の位置を【全部】返す(無ければ空)。
933
956
 
934
957
  ★ここで返した位置だけをパスの判定から外す。それ以外は今までどおり全部測る。
935
958
  ★外すのは「探す文字」だけ:名指しのパス(-f / --file の値を含む)は1つも外さない。
959
+ ★seg(測っている区切りの字面)を渡すと、語の割り方が素朴分割へ倒れたかを見て、
960
+ 割れた探す言葉の続きも外す(便 362a7759)。渡さなければ続きを1つも外さない
961
+ =パスとして測る側が増える安全側。
936
962
  """
937
963
  if verb not in _PATTERN_FIRST_VERBS:
938
964
  return frozenset()
965
+ naive = naive_token_split(seg)
939
966
  out = set()
940
967
  explicit = False
941
968
  i, n = 0, len(args)
@@ -948,20 +975,20 @@ def pattern_arg_positions(verb: str, args):
948
975
  explicit = True
949
976
  if i + 1 < n:
950
977
  # `-e 語` の値は探す文字(パスではない)。値も引用符の壊れで割れることがある。
951
- out.update(_pattern_word_positions(args, i + 1))
978
+ out.update(_pattern_word_positions(args, i + 1, naive=naive))
952
979
  i += 2
953
980
  continue
954
981
  elif t.startswith('--regexp='):
955
982
  explicit = True
956
983
  # `--regexp=語` は旗に探す文字が埋まっている(開き具合は `=` の後ろだけで見る)。
957
- out.update(_pattern_word_positions(args, i, t.split('=', 1)[1]))
984
+ out.update(_pattern_word_positions(args, i, t.split('=', 1)[1], naive=naive))
958
985
  i += 1
959
986
  if explicit:
960
987
  return frozenset(out) # 探す文字は旗で示されている=最初の引数はパス
961
988
  first = next((p for p, t in enumerate(args) if not t.startswith('-')), -1)
962
989
  if first < 0:
963
990
  return frozenset()
964
- return frozenset(_pattern_word_positions(args, first))
991
+ return frozenset(_pattern_word_positions(args, first, naive=naive))
965
992
 
966
993
 
967
994
  # 「中身を読み出す」動詞(秘密に対して使われたら露出)。
@@ -1628,7 +1655,7 @@ def _recursive_read_danger(verb: str, tokens, cwd=None, seg=None):
1628
1655
  else:
1629
1656
  return None
1630
1657
  patterns = _search_excludes(args)
1631
- skips = pattern_arg_positions(verb, args) # 探す文字は【全部】パスの判定から外す
1658
+ skips = pattern_arg_positions(verb, args, seg) # 探す文字は【全部】パスの判定から外す
1632
1659
  roots = []
1633
1660
  files = []
1634
1661
  named = 0
@@ -1673,7 +1700,13 @@ def _recursive_read_danger(verb: str, tokens, cwd=None, seg=None):
1673
1700
 
1674
1701
 
1675
1702
  def _find_feed_danger(command: str, cwd=None):
1676
- """`find <場所> -exec cat {}` や `find <場所> | xargs cat` の形で、場所の中に秘密が在れば理由を返す。"""
1703
+ """`find <場所> -exec cat {}` や `find <場所> | xargs cat` の形で、場所の中に秘密が在れば理由を返す。
1704
+
1705
+ ★名指しの先が【実在のファイル】だけなら、並べられるのはそのファイルだけなので
1706
+ 作業場所に倒さない(便 362a7759・便 3be099ba の原因①と同じ考え方=
1707
+ 実在が確かめられたときだけ狭める)。その代わり、その段で実在の秘密のファイルを
1708
+ 名指ししていれば今までどおり止める(-exec の側に名前を書く形も見る)。
1709
+ """
1677
1710
  low = command
1678
1711
  if not re.search(r'\bfind\b', low):
1679
1712
  return None
@@ -1693,12 +1726,27 @@ def _find_feed_danger(command: str, cwd=None):
1693
1726
  continue
1694
1727
  args = toks[1:]
1695
1728
  roots = []
1729
+ files = []
1696
1730
  for a in args:
1697
1731
  if a.startswith('-') or a in ('(', ')', '!'):
1698
1732
  break
1699
1733
  d = _existing_dir(a, cwd)
1700
1734
  if d:
1701
1735
  roots.append(d)
1736
+ continue
1737
+ f = _existing_file(a, cwd)
1738
+ if f:
1739
+ files.append(f)
1740
+ if not roots and files:
1741
+ # ★名指しの先が実在のファイルだけ=並べられるのはそのファイルだけなので倒さない。
1742
+ # その段で名指しした実在のファイルが秘密なら、今までどおり止める
1743
+ # (名指しの先そのものと、-exec の側に書いた名前の両方を見る)。
1744
+ for a in args:
1745
+ f = _existing_file(a, cwd)
1746
+ sr = secret_reason(f) if f else None
1747
+ if sr:
1748
+ return f'{sr} read through find'
1749
+ continue
1702
1750
  if not roots:
1703
1751
  base = cwd if (cwd and os.path.isabs(cwd)) else os.getcwd()
1704
1752
  roots = [os.path.realpath(base)]
@@ -3513,7 +3561,7 @@ def _secret_danger(segment: str, verb: str, tokens, cwd=None):
3513
3561
  args = tokens[1:] if tokens else []
3514
3562
  # ★探す文字(grep の1つ目の引数・-e / --regexp の値・引用符が壊れて割れた続き)は
3515
3563
  # パスではないので、パスの判定から外す(便 18e9e32e)。
3516
- skips = pattern_arg_positions(verb, args)
3564
+ skips = pattern_arg_positions(verb, args, segment)
3517
3565
  for pos, raw in enumerate(args):
3518
3566
  if pos in skips:
3519
3567
  continue
@@ -5200,6 +5248,16 @@ def _disc_is_whole(target, cwd):
5200
5248
  return t in wide
5201
5249
 
5202
5250
 
5251
+ #: 探す文字(またはそれが書いてあるファイル)を旗で明示する書き方のうち、
5252
+ #: 値を `=` で繋げて1語にした形(`--regexp=語` / `--file=パス`)。
5253
+ #: ★これが在るときも「探す文字は旗で示されている」=旗でない引数はすべて【探す場所】。
5254
+ _DISC_PATTERN_JOINED = ('--regexp=', '--file=')
5255
+
5256
+
5257
+ def _disc_pattern_joined(w) -> bool:
5258
+ return any(w.startswith(p) for p in _DISC_PATTERN_JOINED)
5259
+
5260
+
5203
5261
  def _disc_search(words, cwd):
5204
5262
  if not words:
5205
5263
  return None
@@ -5220,6 +5278,10 @@ def _disc_search(words, cwd):
5220
5278
  if w in ('-e', '-f', '--regexp', '--file'):
5221
5279
  has_e, skip = True, True
5222
5280
  continue
5281
+ if _disc_pattern_joined(w):
5282
+ # `--regexp=語` / `--file=パス` … 探す文字は旗に埋まっている(便 220a1e2c)。
5283
+ has_e = True
5284
+ continue
5223
5285
  if w.startswith('-'):
5224
5286
  continue
5225
5287
  pos.append(w)
@@ -5235,6 +5297,10 @@ def _disc_search(words, cwd):
5235
5297
  if w in ('-e', '-f', '--regexp', '--file'):
5236
5298
  has_e, skip = True, True
5237
5299
  continue
5300
+ if _disc_pattern_joined(w):
5301
+ # `--regexp=語` / `--file=パス` … 探す文字は旗に埋まっている(便 220a1e2c)。
5302
+ has_e = True
5303
+ continue
5238
5304
  if w in ('--max-depth', '-d', '--depth'):
5239
5305
  depth, skip = 'next', True
5240
5306
  continue