Ashi <[email protected]> writes:

> Hi, Michael,

Hi Zhongwei,

> This update includes following change:
> - Fix the auto completion bug.

Now that your FSF assignment is complete (congratulations!), I did a
more granular review + test of your patch. Some remarks:

- I still would like to call the entry in tramp-methods tramp-default-port.
  We shall not introduce different entries for the same thing, and
  tramp-default-port does exist already.

- I've added traces to tramp-adb-parse-device-names, and now it's
  visible that this is called very often. Not so good with performance
  in mind; maybe we could do something in the future to change it. Call
  it less often, cache the result, whatever.

  I have also changed it to return "host#port" entries. So we could work
  with this syntax internally. By this, I could remove
  tramp-adb-parse-device-names-for-completion.

- I have rewritten tramp-adb-get-host-for-execution (new name is
  tramp-adb-get-device) in order to simplify it. Works for me, but I'm
  not sure whether it is OK for all use cases. Please check!

  It uses now also caching. tramp-adb-search-host-in-devices is not
  needed anymore.

- In tramp-adb-maybe-open-connection I have applied changes resulting
  from the previous ones. I have also removed some checks for devices;
  hopefully I haven't removed too much.

Could you, please, test the updated patch? If it is fine with you, I
would commit it in your name. I would need ChangeLog entries from you.

> Best regards,
> Zhongwei 

Best regards, Michael.

*** /home/albinus/src/tramp/lisp/tramp-adb.el.~master~  2015-01-14 
16:19:50.646342876 +0100
--- /home/albinus/src/tramp/lisp/tramp-adb.el   2015-01-14 16:19:05.139556547 
+0100
***************
*** 44,49 ****
--- 44,56 ----
    :version "24.4"
    :type 'string)
  
+ (defcustom tramp-adb-connect-if-not-connected nil
+   "Try to run `adb connect' if provided device is not connected currently.
+ It is used for TCP/IP devices."
+   :group 'tramp
+   :version "25.1"
+   :type 'boolean)
+ 
  ;;;###tramp-autoload
  (defconst tramp-adb-method "adb"
    "*When this method name is used, forward all calls to Android Debug 
Bridge.")
***************
*** 70,76 ****
  ;;;###tramp-autoload
  (add-to-list 'tramp-methods
             `(,tramp-adb-method
!              (tramp-tmpdir "/data/local/tmp")))
  
  ;;;###tramp-autoload
  (add-to-list 'tramp-default-host-alist `(,tramp-adb-method nil ""))
--- 77,84 ----
  ;;;###tramp-autoload
  (add-to-list 'tramp-methods
             `(,tramp-adb-method
!              (tramp-tmpdir "/data/local/tmp")
!                (tramp-default-port 5555)))
  
  ;;;###tramp-autoload
  (add-to-list 'tramp-default-host-alist `(,tramp-adb-method nil ""))
***************
*** 78,84 ****
  ;;;###tramp-autoload
  (eval-after-load 'tramp
    '(tramp-set-completion-function
!     tramp-adb-method '((tramp-adb-parse-device-names ""))))
  
  ;;;###tramp-autoload
  (add-to-list 'tramp-foreign-file-name-handler-alist
--- 86,92 ----
  ;;;###tramp-autoload
  (eval-after-load 'tramp
    '(tramp-set-completion-function
!     tramp-adb-method '((tramp-adb-parse-device-names-for-completion ""))))
  
  ;;;###tramp-autoload
  (add-to-list 'tramp-foreign-file-name-handler-alist
***************
*** 181,195 ****
        ;; `call-process' does not react on timer under MS Windows.
        ;; That's why we use `start-process'.
        (let ((p (start-process
!               tramp-adb-program (current-buffer) tramp-adb-program "devices"))
            result)
        (tramp-compat-set-process-query-on-exit-flag p nil)
        (while (eq 'run (process-status p))
          (accept-process-output p 0.1))
        (accept-process-output p 0.1)
        (goto-char (point-min))
        (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t)
          (add-to-list 'result (list nil (match-string 1))))
        result))))
  
  (defun tramp-adb-handle-expand-file-name (name &optional dir)
--- 189,217 ----
        ;; `call-process' does not react on timer under MS Windows.
        ;; That's why we use `start-process'.
        (let ((p (start-process
!               tramp-adb-program (current-buffer)
!               tramp-adb-program "devices"))
!           (v (vector tramp-adb-method tramp-current-user
!                      tramp-current-host nil nil))
            result)
+       (tramp-message v 6 "%s" (mapconcat 'identity (process-command p) " "))
        (tramp-compat-set-process-query-on-exit-flag p nil)
        (while (eq 'run (process-status p))
          (accept-process-output p 0.1))
        (accept-process-output p 0.1)
+       (tramp-message v 6 "\n%s" (buffer-string))
        (goto-char (point-min))
        (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t)
          (add-to-list 'result (list nil (match-string 1))))
+ 
+       ;; Replace ":" by "#".
+       (mapc
+        (lambda (elt)
+          (setcar
+           (cdr elt)
+           (replace-regexp-in-string
+            ":" tramp-prefix-port-format (car (cdr elt)))))
+        result)
        result))))
  
  (defun tramp-adb-handle-expand-file-name (name &optional dir)
***************
*** 989,1000 ****
          (tramp-set-connection-property v "process-name" nil)
          (tramp-set-connection-property v "process-buffer" nil))))))
  
! ;; Helper functions.
  
  (defun tramp-adb-execute-adb-command (vec &rest args)
    "Returns nil on success error-output on failure."
!   (when (> (length (tramp-file-name-host vec)) 0)
!     (setq args (append (list "-s" (tramp-file-name-host vec)) args)))
    (with-temp-buffer
      (prog1
        (unless
--- 1011,1064 ----
          (tramp-set-connection-property v "process-name" nil)
          (tramp-set-connection-property v "process-buffer" nil))))))
  
! (defun tramp-adb-get-device (vec)
!   "Return full host name from VEC to be used in shell exceution.
! E.g. a host name \"192.168.1.1#5555\" returns \"192.168.1.1:5555\"
!      a host name \"R38273882DE\" returns \"R38273882DE\"."
!   ;; Sometimes this is called before there is a connection process
!   ;; yet.  In order to work with the connection cache, we flush all
!   ;; unwanted entries first.
!   (tramp-flush-connection-property nil)
!   (with-tramp-connection-property (tramp-get-connection-process vec) "device"
!     (let* ((method (tramp-file-name-method vec))
!          (host (tramp-file-name-host vec))
!          (port (tramp-file-name-port vec))
!          (devices (mapcar 'cadr (tramp-adb-parse-device-names nil))))
!       ;; Checking whether exe-name is in devices. Follow cases are checked:
!       ;;  - yes, correct exe-name returns.
!       ;;  - not, true device name is: xxxx
!       ;;    user types: xxxx:port-number. Error returns with tips of correct 
name
!       ;;  - not, true device name is: xxxx:port-number
!       ;;    user types: xxxx. Tramp will auto-complete port-number for user
!       ;;  - not, no device name is similar as user gives. if
!       ;;    tramp-adb-connect-if-not-connected is t, try to connect that 
device,
!       ;;    else return error
!       (replace-regexp-in-string
!        tramp-prefix-port-format ":"
!        (cond ((member host devices) host)
!            ;; This is the case when the host is connected to the default port.
!            ((member (format "%s%s%d" host tramp-prefix-port-format port)
!                     devices)
!             (format "%s:%d" host port))
!            ;; An empty host name shall be mapped as well, when there
!            ;; is exactly one entry in `devices'.
!            ((and (zerop (length host)) (= (length devices) 1))
!             (car devices))
!            ;; Try to connect device.
!            ((and tramp-adb-connect-if-not-connected
!                  (not (zerop (length host)))
!                  (tramp-adb-execute-adb-command
!                   vec "connect" (format "%s:%s" host port)))
!             (format "%s:%d" host port))
!            (t (tramp-error
!                vec 'file-error "Could not find device %s" host)))))))
  
  (defun tramp-adb-execute-adb-command (vec &rest args)
    "Returns nil on success error-output on failure."
!   (when (and (> (length (tramp-file-name-host vec)) 0)
!            ;; The -s switch is only available for ADB device commands.
!            (not (member (car args) (list "connect" "disconnect"))))
!     (setq args (append (list "-s" (tramp-adb-get-device vec)) args)))
    (with-temp-buffer
      (prog1
        (unless
***************
*** 1097,1103 ****
         (p (get-buffer-process buf))
         (host (tramp-file-name-host vec))
         (user (tramp-file-name-user vec))
!        devices)
  
      ;; Maybe we know already that "su" is not supported.  We cannot
      ;; use a connection property, because we have not checked yet
--- 1161,1172 ----
         (p (get-buffer-process buf))
         (host (tramp-file-name-host vec))
         (user (tramp-file-name-user vec))
!          (device (tramp-adb-get-device vec))
! 
!     ;; Set variables for proper tracing in `tramp-adb-parse-device-names'.
!     (setq tramp-current-method (tramp-file-name-method vec)
!         tramp-current-user   (tramp-file-name-user vec)
!         tramp-current-host   (tramp-file-name-host vec))
  
      ;; Maybe we know already that "su" is not supported.  We cannot
      ;; use a connection property, because we have not checked yet
***************
*** 1109,1128 ****
        (and p (processp p) (memq (process-status p) '(run open)))
        (save-match-data
        (when (and p (processp p)) (delete-process p))
!       (setq devices (mapcar 'cadr (tramp-adb-parse-device-names nil)))
!       (if (not devices)
!           (tramp-error vec 'file-error "No device connected"))
!       (if (and (> (length host) 0) (not (member host devices)))
            (tramp-error vec 'file-error "Device %s not connected" host))
-       (if (and (> (length devices) 1) (zerop (length host)))
-           (tramp-error
-            vec 'file-error
-            "Multiple Devices connected: No Host/Device specified"))
        (with-tramp-progress-reporter vec 3 "Opening adb shell connection"
          (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
                 (process-connection-type tramp-process-connection-type)
                 (args (if (> (length host) 0)
!                          (list "-s" host "shell")
                         (list "shell")))
                 (p (let ((default-directory
                            (tramp-compat-temporary-file-directory)))
--- 1178,1190 ----
        (and p (processp p) (memq (process-status p) '(run open)))
        (save-match-data
        (when (and p (processp p)) (delete-process p))
!       (if (zerop (length device))
            (tramp-error vec 'file-error "Device %s not connected" host))
        (with-tramp-progress-reporter vec 3 "Opening adb shell connection"
          (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
                 (process-connection-type tramp-process-connection-type)
                 (args (if (> (length host) 0)
!                          (list "-s" device "shell")
                         (list "shell")))
                 (p (let ((default-directory
                            (tramp-compat-temporary-file-directory)))
_______________________________________________
Tramp-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/tramp-devel

Reply via email to